Following up form my last question -
var socket;
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
socket = new WebSocket("ws://localhost:10001");
}
socket.onopen() = function(){
alert("Connection Opened");
}
socket.onmessage() = function(msg){
alert(msg);
}
I can connect to the server with telnet but I can't seem to connect using Javascript, why is this?
Because WebSocket is not a normal, general-purpose socket. It requires the server on the remote end to conform to a very specific handshake defined by the WebSocket protocol . If your server does not implement this protocol, WebSocket cannot connect to it.
Additionally, as Rocket points out, your code is currently attempting to call socket.onopen()
and assign a value to the function call. Lose the parentheses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.