简体   繁体   中英

Websocket won't open connection

I have been battling with websockets for the last few hours and can't get it too connect!

These are the handshaking protocols:

GET /websocket/Server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:10020
Origin: http://localhost:8888
Sec-WebSocket-Key: fWN5C0N5EqQ/EnYET9C8DQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cookie: SQLiteManager_currentLangue=2

MY RESPONSE:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: P3XPDvAuKuz0RoHKq8oDrGHRM9c=

The PHP function I use to write the response is:

function handle13Protocol($req){
  $info = array();
  $MAGICSTRING = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  if(preg_match("/GET (.*) HTTP/"   ,$req,$match)){ $info["GET"]=$match[1]; }
  if(preg_match("/Host: (.*)\r\n/"  ,$req,$match)){ $info["HOST"]=$match[1]; }
  if(preg_match("/Origin: (.*)\r\n/",$req,$match)){ $info["ORIGIN"]=$match[1]; }
  if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)){ $info["KEY"]=$match[1]; }
  if(preg_match("/\r\n(.*?)\$/",$req,$match)){ $data=$match[1]; }

  echo "key = ".$info["KEY"]."\n";

  $acceptKey = base64_encode(sha1($info["KEY"].$MAGICSTRING,true));

  $upgrade =  "HTTP/1.1 101 Switching Protocols\r\n" .
               "Upgrade: websocket\r\n" .
               "Connection: Upgrade\r\n" .
               "Sec-WebSocket-Accept: $acceptKey".
                "\r\n\r\n" ;
  echo $upgrade;

  return $upgrade;  

}

On my console it does not come up with any errors, and it says the socket does not disconnect.

Yet my javascript won't confirm my connection ie onopen function. I even tried this copy and paste section to ensure I have the right js:

alert("Connecting to server now");

function init(){
  var host = "ws://localhost:10020/websocket/Server.php";

  try{
    socket = new WebSocket(host);
    log('WebSocket - status '+socket.readyState);
    socket.onopen    = function(msg){ alert("opened!"); };
    socket.onmessage = function(msg){ log("Received: "+msg.data); };
    socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };
  }
  catch(ex){ log(ex); }
  $("msg").focus();
}

The problem was in the javascript. The correct JS is

alert("Connecting to server now");

function init(){
  var host = "ws://localhost:10020/websocket/Server.php";

  try{
    socket = new WebSocket(host);
    socket.onopen    = function(msg){ alert("opened!"); };
    socket.onmessage = function(msg){ log("Received: "+msg.data); };
    socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };
  }
  catch(ex){ log(ex); }
  $("msg").focus();
}

With the log method removed, as this was not behaving correctly and throwing an error...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM