made a mobile phone app, using socket io as a long connection. During the test, I found that there were occasional continuous connections / disconnections. I printed a message on the console as shown in the figure,
. During the test, there are multiple client terminals connected, but only one is like this. My code is as follows:
const http = require ("http");
let server = http.createServer (function (req,res) {
res.writeHead(200,{"Content-Type":"text/html"});
res.end("connected","utf-8");
}) .cake (ioConf.port,ioConf.host);
lightLog.info ("pid",process.pid,"SocketIO Server running at",ioConf);
let io = require ("socket.io"). Upload (server);
let sockets = []; / / Save all connections from the web
io.sockets.on (" connection", function (socket) {/ / new connections
// lightLog.info(socket);
let connectionKey = socket.handshake.address + ":" + socket.id;
lightLog.info(":",connectionKey);
//
socket.on("new message",function(data) {//json
let obj = {};
let realData = JSON.parse(data);
let pushMessage = realData.group_name;
lightLog.info(pushMessage);
socket.emit(pushMessage,data);
socket.broadcast.emit(pushMessage,data);//
// lightLog.info(":"+connectionKey+":"+data);
lightLog.info(":"+connectionKey+":"+data+"\r\n");
let result = realData.type;//
// lightLog.info(result);
try{
// require("../router/"+result+".js").receive(
// data.userID,data.flag,data.type,data.Num,data.value1,data.value2);
}catch(err){
lightLog.error("pid: ",process.pid," :"," : \r\n ",err.stack);
}
// lightLog.info(data);
//
let sql = util.format("insert into %s (group_name,userID,login_name,message,time,type,user_name) " +
" values("%s","%s","%s","%s","%s","%s","%s") ",chat_record,realData.group_name,realData.id,realData.login_name,
realData.message,realData.time,realData.type,realData.user_name);
lightLog.info(sql);
mysqlClient(sql,function(err,res) {
if(err) {
lightLog.error("pid:",process.pid," \r\n",err.stack);
}else{
lightLog.debug("pid:",process.pid,"",res);
}
});
});
//
//
socket.on("add user",function(data) {
lightLog.info(":"+connectionKey+":"+data+"\r\n");
// let realData = JSON.parse(data);
let objUser = {};
objUser.user_name = data;
objUser.time = moment().format("YYYY/MM/DD HH:mm:ss");
//
lightLog.info(objUser);
socket.emit("user joined",objUser);
socket.broadcast.emit("user joined",objUser);
// IPredis
redisClient.sadd("userName",connectionKey,function(err,res) {
});
});
// lightLog.info("pid",process.pid,":",connectionKey);
//
socket.on("user exit", function (data) {//
lightLog.info("pid",process.pid,":","",data);
socket.emit("user exit",);
socket.broadcast.emit("user exit",);
//
redisClient.del("userName:"+connectionKey,function(err,res) {
});
});
//
socket.on("disconnect", function () {//
lightLog.info("pid",process.pid,":",connectionKey,"");
let index = sockets.indexOf(socket);
// socket.emit("user out",objUser);
// socket.broadcast.emit("user out",objUser);
redisClient.srem("userName",connectionKey,function(err,res) {
});
});
});
which god do you want to help me see what the reason is? If there is something wrong with the test, what do you need to do?