[英]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.