This commit is contained in:
theluyuan 2020-12-09 14:34:11 +08:00
parent 6b28472d72
commit d498fa0080
6 changed files with 131 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}

35
index.js Normal file
View File

@ -0,0 +1,35 @@
var dgram = require('dgram');
var bencode = require('bencode');
//创建 udp server
var udp_server = dgram.createSocket('udp4');
// 绑定端口
udp_server.bind(8888);
// 绑定端口(自定义)
const clients = {};
// 监听端口
udp_server.on('listening', function () {
console.log('UDP启动监听');
});
//接收消息
udp_server.on('message', function (msg, rinfo) {
msg = bencode.decode(msg);
clients[msg.id] = rinfo;
// 将以记录的请求数据全部发送给请求的客户端
let buf = bencode.encode({
address: rinfo.address,
port: rinfo.port,
clients: clients,
data: msg.data,
id: msg.id,
type: "server"
});
udp_server.send(buf, 0, buf.length, rinfo.port, rinfo.address); //将接收到的消息返回给客户端
console.log(`客户端 ${rinfo.address}:${rinfo.port} 发送请求`);
});
//错误处理
udp_server.on('error', function (err) {
console.log(err);
udp_server.close();
});

56
kehuduan.js Normal file
View File

@ -0,0 +1,56 @@
const dgram = require('dgram');
const bencode = require('bencode');
// 用时间戳模拟id标识
const ID = (+new Date()) + "";
//创建UDP服务
const udp_server = dgram.createSocket('udp4');
// 绑定随机端口
udp_server.bind();
// 监听端口
udp_server.on('listening', function (a) {
console.log('UDP启动监听');
});
// 客户端信息,以及测试用的客户端信息
let clients, testClient;
//接收消息
udp_server.on('message', function (msg, rinfo) {
var data = bencode.decode(msg);
if (data.type == "server") {
clients = data.clients;
//获取测试的客户端
for (let k in clients) {
if (k !== ID) {
testClient = clients[k];
break;
}
}
} else if (data.type == "client") {
//显示获取的信息
console.log((new Date()).toLocaleString(), data.id.toString(), rinfo.address.toString(), data.data.toString());
}
})
//错误处理
udp_server.on('error', function (err) {
console.log(err);
udp_server.close();
});
// 向服务器发送消息
function sendServer() {
var buff = bencode.encode({ id: ID, data: "Hello Server", type: "client" });
// 自定义发送 服务器 外网端口与ip
udp_server.send(buff, 0, buff.length, 8888, '127.0.0.1');
}
sendServer();
// 向其他客户端发送测试数据
function sendClient() {
if (testClient) {
var buff = bencode.encode({ id: ID, data: "Hello Client", type: "client" });
udp_server.send(buff, 0, buff.length, testClient.port, testClient.address.toString());
}
}
// 定时发送服务器(发送心跳包)
setInterval(sendServer, 10000);
// 发送测试数据
setInterval(sendClient, 2000);

9
package.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "udpdadong",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"bencode": "^2.0.1"
}
}

15
yarn.lock Normal file
View File

@ -0,0 +1,15 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
bencode@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/bencode/-/bencode-2.0.1.tgz#667a6a31c5e038d558608333da6b7c94e836c85b"
integrity sha512-2uhEl8FdjSBUyb69qDTgOEeeqDTa+n3yMQzLW0cOzNf1Ow5bwcg3idf+qsWisIKRH8Bk8oC7UXL8irRcPA8ZEQ==
dependencies:
safe-buffer "^5.1.1"
safe-buffer@^5.1.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==