Jump to content

Will this work as a websocket server?


wh33t

Recommended Posts

Hey Freaks.

 

I'm trying to build a web based card game. I was about to begin writing some ajax functionality in the client when I someone suggested to me I look into websockets. It seems like the information out there for a php based socket server are either based upon some framework or library or really old. But after various tutorials I've come up with the following.

 

This is socket.php, it's located at the document root of the server.

<?php
$server = stream_socket_server("tcp://192.168.0.3:9000", $errno, $errorMessage);

if ($server === false) {
    throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}

while(true) {
    
    $client = stream_socket_accept($server,-1);

    if ($client) {
        $recieved_message = fread($client,1024);
        fwrite($client,'You said ' . $recieved_message);
        stream_copy_to_stream($client, $client);
        fclose($client);
    }
}
?>

So I have to ask, is this a websocket server? When it is running, I can shell into my local development server and run this command

echo "Hi Server" | nc 192.168.0.3 9000

And I get the expected response "You said Hi Server".

 

However, when I try to connect to from Firefox with the following code.

<html>
  <head>
  <script type="text/javascript">
      function socket_connect()
      {
        //create a new WebSocket object.
        socket = new WebSocket("ws://192.168.0.3:9000/socket.php");
        socket.onopen = function(evt) { console.log('Open Event'); }; //on open event
        socket.onclose = function(evt) { console.log('Socket Closed'); }; //on close event
        socket.onmessage = function(evt) { console.log('Message Recieved'); }; //on message event
        socket.onerror = function(error) { console.log('Error Recieved'); }; //on error event
        socket.close(); //close method            
      }
    </script>
  </head>
<body onload="socket_connect();">

</body>
 
</html>

I get these two error messages in the browser console and I have no clue what I should troubleshoot next.

Firefox can’t establish a connection to the server at ws://d30:9000/socket.php.
The connection to ws://d30:9000/socket.php was interrupted while the page was loading.

Also, just for its worth. I have disabled my firewall on my local server just to rule it out. Same issue still occurs.

 

Any advice or tips is greatly appreciated. Thank you.

Link to comment
Share on other sites

No, it's not.

 

Websocket is a specific protocol. You have to implement that protocol if you want a Websocket server. If you do some searching around you can probably find some implementations that already exist. I don't have any specific recommendations.

Link to comment
Share on other sites

No, it's not.

 

Websocket is a specific protocol. You have to implement that protocol if you want a Websocket server. If you do some searching around you can probably find some implementations that already exist. I don't have any specific recommendations.

 

Thank you! I needed that clarification. I'll just use an existing framework such as Ratchet.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.