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
    //参照
  • 相关阅读:
    拖拽改变元素位置或大小bug修复
    拖拽改变元素位置或大小
    vue使用input上传多张图片,可以预览
    javaScript面向对象编程学习(二)
    移动端适配字体大小
    js的时间戳的转换
    vue-cli3.X中的vue.config.js的配置
    javaScript面向对象编程学习(一)
    Visual Studio code 常用插件集集合。
    js的四个小练习
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/15709448.html
Copyright © 2011-2022 走看看