var ws = require("nodejs-websocket");
console.log("开始建立连接...")
// let serverlist = [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null];
var game1 = null,game2 = null , game1Ready = false , game2Ready = false;
let mb_serverobj = {};//手机版的 进程对象
let pc_serverobj = {};
var server = ws.createServer(function(conn){
conn.on("text", function (str) {
console.log("收到的信息为:"+str)
if(str==="game1"){
game1 = conn;
game1Ready = true;
conn.sendText("success");
}
if(str==="game2"){
game2 = conn;
game2Ready = true;
}
if(game1Ready&&game2Ready){
game2.sendText(str);
}
//创建进程
if(str.indexOf("mbwsitem_")!=-1){
//身份
// if(!mb_serverobj[str]){
console.info("手机创建身份",str);
mb_serverobj[str] = conn;
// }
}
//创建进程
if(str.indexOf("pcwsitem_")!=-1){
// if(!pc_serverobj[str]){
console.info("PC创建身份",str);
pc_serverobj[str] = conn;
// }
}
//发送拨打电话请求
if(str.indexOf("tophone")!=-1){
let jsonres = JSON.parse(str);
console.info("给手机版的发消息");
if(mb_serverobj['mbwsitem_'+jsonres.myphone]){
console.info("给手机版的发消息1");
mb_serverobj['mbwsitem_'+jsonres.myphone].sendText(str);//给手机版的发消息
}else{
console.info("mb_serverobj",mb_serverobj);
console.info("str:",'mbwsitem_'+jsonres.myphone);
console.info("未注册!",mb_serverobj['mbwsitem_'+jsonres.myphone]);
}
}
conn.sendText(str)
})
conn.on("close", function (code, reason) {
console.log("关闭连接")
});
conn.on("error", function (code, reason) {
console.log("异常关闭")
});
}).listen(8001)
console.log("WebSocket建立完毕")
//https://www.cnblogs.com/shaozhu520
//参照