Unable to connect web socket with javascript

I want to connect to my web socket, which will add an amazone instance with some ip. I can connect my web socket with some ip and port with google rest client application and it works very well. Screenshot: enter image description here

But if I want to associate this with a java script, it will not be able to connect. It works up to 2-3 months. I have not changed and nothing, but now it does not work. If I want to connect to firefox, this will result in an error. Here is my code: -

function init() { var host = "ws://XX.XX.XXX.XXX:XXXX"; // SET THIS TO YOUR SERVER try { var socket = new WebSocket(host); // alert('WebSocket - status ' + socket.readyState); log('WebSocket - status ' + socket.readyState); socket.onopen = function (msg) { alert('open'); alert("Welcome - status " + this.readyState); log("Welcome - status " + this.readyState); if (this.readyState != 1) { reconnect(); } }; socket.onmessage = function (msg) { // alert("Received: " + msg.data); log("Received: " + msg.data); }; socket.onclose = function (msg) { // alert("Disconnected - status " + this.readyState); log("Disconnected - status " + this.readyState); }; } catch (ex) { alert(ex); log(ex); } $("msg").focus(); } 

This is the alert status 0 and error display in the console: -

 Firefox can't establish a connection to the server at ws://XX.XX.XXX.XXX:XXXX. var socket = new WebSocket(host); 
+5
source share
1 answer

I would try your code and everything works fine for me, I would test it on this web page: https://www.websocket.org/echo.html , maybe it can be useful for testing purposes. But also I found this question: websocket-rails, a websocket handshake error might also help. However, I would just change the host in your code to this: "ws: //echo.websocket.org", and everything works without problems. I hope you find a solution and that this information was useful. Here is your code that I used for the test:

 function init() { var host = "ws://echo.websocket.org"; try { var socket = new WebSocket(host); alert('WebSocket - status ' + socket.readyState); socket.onopen = function (msg) { alert('open'); alert("Welcome - status " + this.readyState); if (this.readyState != 1) { reconnect(); } }; socket.onmessage = function (msg) { alert("Received: " + msg.data); }; socket.onclose = function (msg) { alert("Disconnected - status " + this.readyState); }; } catch (ex) { alert(ex); } $("msg").focus(); } 

* Sorry for my bad english.

0
source

All Articles