Question

Firefox can't establish a connection to the server at ws://192.168.0.155:5555/socket/server3.php.

$(document).ready(function(){
if(!("WebSocket" in window)){
alert('not available');
}else{
_init(); 
}
});
function _init(){
     var websocket;
     var host = 'ws://192.168.0.155:5555/socket/server3.php';
     try{
     websocket = new WebSocket(host);
     websocket.onopen = function(evt){ onOpen(evt); };
     websocket.onclose = function(evt) { onClose(evt); };
     websocket.onmessage = function(evt) { onMessage(evt); };
     }catch(exception){
     alert(exception);
     }
     }

  function onOpen(evt){
    $('.logger_screen').append('Connected');
      }

  function onClose(evt){
      $('.logger_screen').append('Disconnected');
       }

  function onMessage(evt){
    $('.logger_screen').append(evt.data);
   }

wts wrong with my code ??

Was it helpful?

Solution

From one of your comments, I think you're using phpwebsocket on the server. This project doesn't seem to be maintained and hasn't stayed up to date with changes in the websocket protocol spec.

There are two incompatible versions of the websocket protocol in use. Safari still uses the original (now deprecated) Hixie variant which phpwebsocket implements; Firefox, IE10 and Chrome use the newer Hybi variant.

To test this out, you could try using Safari to to execute your javascript.

I'm not sure what options you have if you want to use PHP on the server and need to support more than Safari. I can see one open source server which should support all the browsers listed above. Alternatively, if you want to try writing your own server, there are quite a few questions posted here, under the websocket tag, that you could take inspiration from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top