关于node的一个问题

大小老鬼

想用小程序和html建立通信,中间用node做服务器,在本地上以及部署好了,在服务器上就提示超时,端口什么的都打开了,从本地到服务器的改动只有改IP为域名和吧ws改为wss。想问一下该怎么解决?
程序如下

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);
            game1.sendText(str);
        }

        conn.sendText(str)
    })
    conn.on("close", function (code, reason) {
        console.log("�ر�����")
    });
    conn.on("error", function (code, reason) {
        console.log("�쳣�ر�")
    });
}).listen(8080)
1945 1 0
1个回答

大小老鬼

这个是连接node的html的代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>

    </style>
</head>
<body>
    <div id="mess"></div>
    <button onclick="nihao()">hhhh</button>

    <script>
        var ws;

        function nihao(){
            ws.send("youmaobjj");
        }
        var mess = document.getElementById("mess");
        if(window.WebSocket){
            var ws = new WebSocket('wss://dxlg.giserhub.com:8080');
            ws.onopen = function(e){
                console.log("连接服务器成功");
                ws.send("game2");
            }
            ws.onclose = function(e){
                console.log("服务器关闭");
            }
            ws.onerror = function(){
                console.log("连接出错");
            }

            ws.onmessage = function(e){
               console.log(e.data)
            }
        }
    </script>
</body>
</html>
  • 暂无评论
年代过于久远,无法发表回答
🔝