1

I'm trying to determine how to setup a web socket for the first time ever so a working minimal example with static variables (IP address for example instead of getservbyname) will help me understand what is flowing where.

I want to do this the right way so no frameworks or addons for both the client and the server. I want to use PHP's native web sockets as described here though without over-complicating things with in-depth classes...

http://www.php.net/manual/en/intro.sockets.php

I've already put together some basic JavaScript...

window.onload = function(e)
{
 if ('WebSocket' in window)
 {
  var socket = new WebSocket('ws://'+path.split('http://')[1]+'mail/');

  socket.onopen = function () {alert('Web Socket: connected.');}

  socket.onmessage = function (event) {alert('Web Socket: '+event.data);} 
 }
}

It's the PHP part that I'm not really sure about. Presuming we have a blank PHP file...

  1. If necessary how do I determine if my server's PHP install has this socket functionality already available?

  2. Is the request essentially handled as a GET or POST request in example?

  3. Do I need to worry about the port numbers? e.g. if ($_SERVER['SERVER_PORT']=='8080')

  4. How do I return a basic message on the initial connection?

  5. How do I return a basic message say, five seconds later?

1 Answer 1

2

It's not that simple to create a simple example, I'm afraid.

First of all you need to check in php configuration if the server is configured for sockets with the setting enable-sockets

Then you need to implement (or find) a websocket server that at least follows the Hybi10 specification (https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-10) of websockets. If you find the "magic number" 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 in the code for the header, you can be sure it does follow at least Hybi06 ...

Finally, you need to have access to an admin console on the server in order to execute the PHP websocket server using php -q server.php

EDIT: This is the one I've been using a year ago ... it might still work as expected with current browsers supporting Websockets: http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/?r=5

Sign up to request clarification or add additional context in comments.

1 Comment

Only took almost three years though came back around to it. I think the key thing I missed was executing the PHP script from the Windows command line: C:\xampp\php\>php -q "D:\web\server.php"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.