繁体   English   中英

无法使用JavaScript建立WebSocket连接

[英]WebSocket connection not establish using JavaScript

这用于热敏打印,当我运行应用程序时,它在控制台中显示以下错误:

jquery.websocket.js:45未捕获的DOMException:无法在“ WebSocket”上执行“发送”:仍处于CONNECTING状态。

码:

 <script type="text/javascript">
     function connect() {
         debugger;
         var ws = new WebSocket("wss://192.168.1.23:9100");
         ws.onopen = function () {
             alert("About to send data");
             ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
             alert("Message sent!");
         };

         ws.onmessage = function (evt) {
             alert("About to receive data");
             var received_msg = evt.data;
             alert("Message received = " + received_msg);
         };
         ws.onclose = function () {
             // websocket is closed.
             alert("Connection is closed...");
         };
     };


</script>

经过您的代码测试,似乎可以正常工作。能否从您的侧面通过'wss://echo.websocket.org'网址进行检查? 我已引用https://www.websocket.org/echo.html网址。 下面是相同的代码

<!DOCTYPE html>
<html>
    <head>
        <title>To test web socket</title>
        <script type="text/javascript">
            function connect() {
                debugger;
                var ws = new WebSocket("wss://echo.websocket.org");
                ws.onopen = function () {
                    alert("About to send data");
                    ws.send("Hello World"); // I WANT TO SEND THIS MESSAGE TO THE SERVER!!!!!!!!
                    alert("Message sent!");
                };

                ws.onmessage = function (evt) {
                   alert("About to receive data");
                   var received_msg = evt.data;
                   alert("Message received = " + received_msg);
                };
                ws.onclose = function () {
                    // websocket is closed.
                   alert("Connection is closed...");
                };
            };
        </script>
    </head>
    <body>
       <button onclick="connect()">Connect</button>
    </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM