Commit bc414cfb by lixinming

no message

parent 13f7dbe5
import { initWSData } from "../../data/interface/wsData"; import { initWSData } from "../../data/interface/wsData";
import { Socket } from "../../net/socket_server"; import { Socket } from "../../net/socket_server";
//连接池
let connectionPool = {}; let connectionPool = {};
//广播消息 //广播消息
export async function sendMsg(data) { export async function sendMsg(data) {
for (let key in connectionPool) { for (let key in connectionPool) {
connectionPool[key].send({code:200, success:true, data}); let result = {code:200, success:true, data}
connectionPool[key].send(JSON.stringify(result));
} }
} }
...@@ -29,18 +32,15 @@ export async function initSocketServer(port) { ...@@ -29,18 +32,15 @@ export async function initSocketServer(port) {
if (data == 'i am heart') ws.send('{success:true, code:199}'); if (data == 'i am heart') ws.send('{success:true, code:199}');
}); });
ws.on('close', function (data, req){ ws.on('close', function (data){
console.log("close", data, req); if ( ws._socket && ws._socket._peername && ws._socket._peername.address ) {
let userId = ws._socket._peername.address;
console.log("close", userId, data);
delete connectionPool[userId];
}
}); });
}); });
}); });
//todo 测试连接代码
test();
}
function test() {
initWSData();
} }
\ No newline at end of file
...@@ -24,7 +24,7 @@ async function sleep(ms) { ...@@ -24,7 +24,7 @@ async function sleep(ms) {
let heartCount = 0; let heartCount = 0;
//心跳 //心跳
let heart = { let heart = {
timeout:4*1000,//40秒一次心跳包 timeout:40*1000,//40秒一次心跳包
serverTimeout:4*1000,//心跳包超时时间 serverTimeout:4*1000,//心跳包超时时间
timeoutObj:null, timeoutObj:null,
notRespontNum:3, //未响应次数阈值 notRespontNum:3, //未响应次数阈值
...@@ -63,28 +63,25 @@ let heart = { ...@@ -63,28 +63,25 @@ let heart = {
} }
//ws客户端 //ws客户端
let path = "ws://192.168.0.105:30002";
let ws = new WebSocket(path, {perMessageDeflate: false});
async function createSocket() { async function createSocket() {
// let path = "ws://10.51.50.136:8090/ws/v1"; let path = "ws://10.51.50.136:8090/ws/v1";
let ws = new WebSocket(path, {perMessageDeflate: false});
ws.onclose = async (err) => { ws.onclose = async (err) => {
// console.log("连接关闭: ", err); console.log("连接关闭: ", err);
// lockReconnect = false; lockReconnect = false;
// heartCount = 0; heartCount = 0;
// //断线重连 //断线重连
// reconnect(); reconnect();
console.log("客户端 关闭"); console.log("客户端 关闭");
} }
ws.onmessage = (msg) => { ws.onmessage = (msg) => {
//接收数据 //接收数据
console.log("客户端接收数据 =>", msg);
heart.start(ws); heart.start(ws);
// stats(msg); stats(msg);
} }
ws.onerror = (error) => { ws.onerror = (error) => {
...@@ -98,11 +95,6 @@ async function createSocket() { ...@@ -98,11 +95,6 @@ async function createSocket() {
} }
setTimeout(() => {
ws.close();
}, 10*1000);
export async function initWSData() { export async function initWSData() {
await createSocket(); await createSocket();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment