我使用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>
有人知道为什么当我使用安全的websocket时会发生这种情况,但代码在使用常规websocket的非HTTPS
页面上运行良好吗?它还通过tomcat 7.0.53
运行。
var ws = new WebSocket("wss://localhost:8080/websocket/echo");
必须更改为用于本地主机的端口,在本例中为8443
,因此它现在应该读取var ws = new WebSocket("wss://localhost:8443/websocket/echo");