* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:*www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const net = require('net');
const HOST = '127.0.0.1';
const PORT = 8979;
const server = net.createServer();
server.listen(PORT, HOST);
console.log('服务端已启动');
const clients = {};
server.on('connection', (socket) => {
onAccept(socket)
});
function onAccept(client) {
console.log(`connected:${client.remoteAddress}:${client.remotePort}`);
delete clients[client.remoteAddress+client.remotePort];
clients[client.remoteAddress+client.remotePort] = client;
console.log(`当前客户端连接数:${Object.keys(clients).length}`);
client.on('data', (data) => {
broadcast(data, client)
});
client.on('end', () => {
console.log(`客户端${client.remoteAddress}:${client.remotePort}已断连`);
});
client.on('close', (hadError) => {
console.log(`客户端${client.remoteAddress}:${client.remotePort}已关闭`);
delete clients[client.remoteAddress+client.remotePort];
console.log(`当前客户端连接数:${Object.keys(clients).length}`);
});
client.on('error', (err) => {
console.log(`客户端错误 ${JSON.stringify(err)}`);
});
}
* 广播消息
*/
function broadcast(msg, self) {
Object.keys(clients).forEach((ip) => {
if (clients[ip].remoteAddress + clients[ip].remotePort != self.remoteAddress + self.remotePort) {
clients[ip].write(msg);
}
});
}