脚本宝典收集整理的这篇文章主要介绍了浅谈websocket,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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 //参照
以上是脚本宝典为你收集整理的浅谈websocket全部内容,希望文章能够帮你解决浅谈websocket所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。