Reputation: 1621
I've been trying to make a websocket chat room that runs in-browser. I've done a bit of googling around and have found a website that provides an example websocket connection (www.websocket.org/echo.html). But whenever I try their "connect" button, it says "disconnected" straight away, and I cannot send a message (this only occurs on Chrome). So I tried it on Firefox, and it says "Error: undefined" and then "disconnected". Both browsers support websocket technology.
So I went to another website (http://www.tutorialspoint.com/html5/html5_websocket.htm) and copied there example code, saved it as ".html" and ran it in chrome. It cannot connect to the websocket. Instead, it says "disconnected".
QUESTION: Why can I not connect to a websocket/why does it keep disconnecting? My Chrome version is 21.0.1180.60.
For further reference, here is my code so far (NOTE: This only is supposed to connect and say that it has connected, not actually chat):
<!doctype html>
<head>
<title>Testing</title>
</head>
<body>
<script type="text/javascript">
function confirm() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
}
else {
alert("Your Browser does not support WebSocket Technology. Please update your Browser.");
}
}
function sendmessage() {
try {
var ws = new WebSocket("ws://(ip of other user/")
}
catch(err)
{
alert("Error with creating the WebSocket")
}
}
</script>
<a href="javascript:confirm()">Run Ip Connector</a>
</body>
</html>
Thanks for any help.
Upvotes: 6
Views: 17273
Reputation: 4376
I just tested the Echo demo on http://websocket.org/echo, and it works fine using Chrome 21. Your other example seems to be working fine as well. You can check it out here.
Since you're using Chrome 21, you can use the Chrome Dev Tools to see if the WebSocket handshake is going through and whether you see any messages.
Is it possible that you're sitting behind a router/firewall that cuts off the traffic? Can you try WSS on websocket.org? (Enter wss://echo.websocket.org in the Location field.)
Upvotes: 3