我使用 HTTPS 连接到以下站点 https://localhost:8443/websocket/
在最新的稳定版 Google Chrome 中,它与我的自签名证书一起正常工作。但是,当我尝试通过页面上的安全 websocket 通过以下 JavaScript 代码发送消息时,我收到了 WebSocket is already in CLOSING or CLOSED state
<meta charset="utf-8">
<title>Tomcat WebSocket Chat</title>
<script>
var ws = new WebSocket("wss://localhost:8080/websocket/echo");
ws.onopen = function(){
};
ws.onmessage = function(message){
document.getElementById("chatlog").textContent += message.data + "\n";
};
function postToServer(){
ws.send(document.getElementById("msg").value);
document.getElementById("msg").value = "";
}
function closeConnect(){
ws.close();
}
</script>
</head>
<body>
<textarea id="chatlog" readonly></textarea><br/>
<input id="msg" type="text" />
<button type="submit" id="sendButton" onClick="postToServer()">Send!</button>
<button type="submit" id="sendButton" onClick="closeConnect()">End</button>
</body>
</html>
当我使用安全的 websockets 但代码在非 HTTPS
上工作正常时,有没有人知道为什么会发生这种情况?带有常规 websockets 的页面?它还正在运行 tomcat 7.0.53
.
最佳答案
var ws = new WebSocket("wss://localhost:8080/websocket/echo");
必须更改为用于本地主机的端口,在本例中为8443
所以它现在应该是 var ws = new WebSocket("wss://localhost:8443/websocket/echo");
关于javascript - 在关闭或已经关闭的模式下保护 websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922147/