Web socket not working in Firefox 12

大兔子大兔子 提交于 2019-12-01 07:09:36

问题


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 ??


回答1:


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.



来源:https://stackoverflow.com/questions/10845502/web-socket-not-working-in-firefox-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!