16e7c69efSopenharmony_ci/*
26e7c69efSopenharmony_ci * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
36e7c69efSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
46e7c69efSopenharmony_ci* you may not use this file except in compliance with the License.
56e7c69efSopenharmony_ci* You may obtain a copy of the License at
66e7c69efSopenharmony_ci*
76e7c69efSopenharmony_ci*     http:*www.apache.org/licenses/LICENSE-2.0
86e7c69efSopenharmony_ci*
96e7c69efSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
106e7c69efSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
116e7c69efSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e7c69efSopenharmony_ci* See the License for the specific language governing permissions and
136e7c69efSopenharmony_ci* limitations under the License.
146e7c69efSopenharmony_ci */
156e7c69efSopenharmony_ci// 模拟流媒体服务,两个client互相发送音频数据
166e7c69efSopenharmony_ci
176e7c69efSopenharmony_ciconst net = require('net');
186e7c69efSopenharmony_ci
196e7c69efSopenharmony_ciconst HOST = '127.0.0.1';
206e7c69efSopenharmony_ciconst PORT = 8979;
216e7c69efSopenharmony_ci
226e7c69efSopenharmony_ciconst server = net.createServer();
236e7c69efSopenharmony_ciserver.listen(PORT, HOST);
246e7c69efSopenharmony_ciconsole.log('服务端已启动');
256e7c69efSopenharmony_ciconst clients = {}; //保存客户端的连接
266e7c69efSopenharmony_ci
276e7c69efSopenharmony_ci// 重要:双方建立链接时,会自动获得一个socket对象(std,socket描述符)
286e7c69efSopenharmony_ciserver.on('connection', (socket) => {
296e7c69efSopenharmony_ci  onAccept(socket)
306e7c69efSopenharmony_ci});
316e7c69efSopenharmony_ci
326e7c69efSopenharmony_cifunction onAccept(client) {
336e7c69efSopenharmony_ci  // 远程客户端地址
346e7c69efSopenharmony_ci  console.log(`connected:${client.remoteAddress}:${client.remotePort}`);
356e7c69efSopenharmony_ci  delete clients[client.remoteAddress+client.remotePort];
366e7c69efSopenharmony_ci  clients[client.remoteAddress+client.remotePort] = client;
376e7c69efSopenharmony_ci  console.log(`当前客户端连接数:${Object.keys(clients).length}`);
386e7c69efSopenharmony_ci  // 收到客户端数据时
396e7c69efSopenharmony_ci  client.on('data', (data) => {
406e7c69efSopenharmony_ci    broadcast(data, client)
416e7c69efSopenharmony_ci  });
426e7c69efSopenharmony_ci  // 客户端主动断连,触发end事件
436e7c69efSopenharmony_ci  client.on('end', () => {
446e7c69efSopenharmony_ci    console.log(`客户端${client.remoteAddress}:${client.remotePort}已断连`);
456e7c69efSopenharmony_ci  });
466e7c69efSopenharmony_ci  client.on('close', (hadError) => {
476e7c69efSopenharmony_ci    console.log(`客户端${client.remoteAddress}:${client.remotePort}已关闭`);
486e7c69efSopenharmony_ci    delete clients[client.remoteAddress+client.remotePort];
496e7c69efSopenharmony_ci    console.log(`当前客户端连接数:${Object.keys(clients).length}`);
506e7c69efSopenharmony_ci  });
516e7c69efSopenharmony_ci
526e7c69efSopenharmony_ci  client.on('error', (err) => {
536e7c69efSopenharmony_ci    console.log(`客户端错误 ${JSON.stringify(err)}`);
546e7c69efSopenharmony_ci  });
556e7c69efSopenharmony_ci}
566e7c69efSopenharmony_ci
576e7c69efSopenharmony_ci/**
586e7c69efSopenharmony_ci * 广播消息
596e7c69efSopenharmony_ci */
606e7c69efSopenharmony_cifunction broadcast(msg, self) {
616e7c69efSopenharmony_ci  Object.keys(clients).forEach((ip) => {
626e7c69efSopenharmony_ci    if (clients[ip].remoteAddress + clients[ip].remotePort != self.remoteAddress + self.remotePort) {
636e7c69efSopenharmony_ci      clients[ip].write(msg);
646e7c69efSopenharmony_ci    }
656e7c69efSopenharmony_ci  });
666e7c69efSopenharmony_ci}