zoukankan      html  css  js  c++  java
  • 浅谈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
    //参照
  • 相关阅读:
    ssm复习资料
    嵌入式开发实践的第二种柱状图代码
    嵌入式开发实践的简单登录代码
    嵌入式开发实践的柱状图代码
    学习ps的坑
    js的执行上下文
    js的渲染
    vue 使用Ueditor富文本的配置
    使用iview Upload进行多文件上传,编辑页初始已上传的图片失败的问题
    beforeEach钩子,next('/login') 跳转问题,无线循环导致Maximum call stack size exceeded问题
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/15709448.html
Copyright © 2011-2022 走看看