zoukankan      html  css  js  c++  java
  • NodeJs 实现 WebSocket 即时通讯(版本一)

    服务端代码

    var ws = require("nodejs-websocket");
    console.log("开始建立连接...")
    
    var game1 = null,game2 = null , game1Ready = false , game2Ready = false;
    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);
            }
    
            conn.sendText(str)
        })
        conn.on("close", function (code, reason) {
            console.log("关闭连接")
        });
        conn.on("error", function (code, reason) {
            console.log("异常关闭")
        });
    }).listen(8001)
    console.log("WebSocket建立完毕")

    客户端代码:

      传数据到服务器

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            .kuang{text-align: center;margin-top:200px;}
            #mess{text-align: center}
            .value{width: 200px;height:200px;border:1px solid;text-align: center;line-height: 200px;display: inline-block;}
        </style>
    </head>
    <body>
        <div id="mess">正在连接...</div>
        <div class="kuang">
            <div class="value" id="value1">小明小明</div>
            <div class="value" id="value2">大胸大胸</div>
            <div class="value" id="value3">小张小张</div>
        </div>
    
        <script>
            var mess = document.getElementById("mess");
            if(window.WebSocket){
                var ws = new WebSocket('ws://127.0.0.1:8001');
    
                ws.onopen = function(e){
                    console.log("连接服务器成功");
                    ws.send("game1");
                }
                ws.onclose = function(e){
                    console.log("服务器关闭");
                }
                ws.onerror = function(){
                    console.log("连接出错");
                }
    
                ws.onmessage = function(e){
                    mess.innerHTML = "连接成功"
                    document.querySelector(".kuang").onclick = function(e){
                        var time = new Date();
                        ws.send(time + "  game1点击了“" + e.target.innerHTML+"");
                    }
                }
            }
        </script>
    </body>
    </html>

      

      接收服务端发送的数据

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            .kuang{text-align: center;margin-top:200px;}
            #mess{text-align: center}
        </style>
    </head>
    <body>
        <div id="mess"></div>
    
        <script>
            var mess = document.getElementById("mess");
            if(window.WebSocket){
                var ws = new WebSocket('ws://127.0.0.1:8001');
    
                ws.onopen = function(e){
                    console.log("连接服务器成功");
                    ws.send("game2");
                }
                ws.onclose = function(e){
                    console.log("服务器关闭");
                }
                ws.onerror = function(){
                    console.log("连接出错");
                }
    
                ws.onmessage = function(e){
                    var time = new Date();
                    mess.innerHTML+=time+"的消息:"+e.data+"<br>"
                }
            }
        </script>
    </body>
    </html>

    来源:https://www.cnblogs.com/axes/p/3586132.html

  • 相关阅读:
    Git
    Entropy, relative entropy and mutual information
    2021.5.3 团队冲刺第六天
    2021.5.2 团队冲刺第五天
    2021.5.1 团队冲刺第四天
    2021.4.30 团队冲刺第三天
    2021.4.29 团队冲刺第二天
    2021.4.28 团队冲刺第一天
    2021.4.27
    2021.4.26
  • 原文地址:https://www.cnblogs.com/zyulike/p/10174779.html
Copyright © 2011-2022 走看看