我使用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但是代码在带有常规websockets的非HTTPS
页面上工作正常时,有没有人知道为什么会这样?它也正在通过tomcat 7.0.53
。
答案 0 :(得分:0)
var ws = new WebSocket("wss://localhost:8080/websocket/echo");
更改为用于本地主机的端口,在本例中为8443
,因此现在应该读取var ws = new WebSocket("wss://localhost:8443/websocket/echo");