Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was in the same boat as you recently, and here is what I did:</p> <p>1) I used the phpwebsockets code as a reference for how to structure the server-side code. (You seem to already be doing this, and as you noted, the code doesn't actually work for a variety of reasons.)</p> <p>2) I used PHP.net to read the details about every socket function used in the phpwebsockets code. By doing this, I was finally able to understand how the whole system works conceptually. This was a pretty big hurdle.</p> <p>3) I read the actual WebSocket draft (please do a web search for it, as I can't post more than two links per post). I had to read this thing a bunch of times before it finally started to sink in. You will likely have to go back to this document again and again throughout the process, as it is the one definitive resource with correct, up-to-date information about the WebSocket API.</p> <p>4) I coded the proper handshake procedure based on the instructions in the draft in #3. This wasn't too bad.</p> <p>5) I kept getting a bunch of garbled text sent from the clients to the server after the handshake and I couldn't figure out why until I realized that the data is encoded and must be unmasked. The following link helped me a lot here: <a href="http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/" rel="noreferrer">http://srchea.com/blog/2011/12/build-a-real-time-application-using-html5-websockets/</a></p> <p>Please note that the code available at this link has a number of problems and won't work properly without further modification.</p> <p>6) I then came across the following SO thread, which clearly explains how to properly encode and decode messages being sent back and forth: <a href="https://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side">How can I send and receive WebSocket messages on the server side?</a></p> <p>This link was really helpful. I recommend consulting it while looking at the WebSocket draft. It'll help make more sense out of what the draft is saying.</p> <p>7) I was almost done at this point, but had some issues with a WebRTC app I was making using WebSocket, so I ended up asking my own question on SO, which I eventually solved. To reference the question and answer, please do a web search for "SO What is this data at the end of WebRTC candidate info?" (without the quotation marks).</p> <p>8) At this point, I pretty much had it all working. I just had to add some additional logic for handling the closing of connections, and I was done.</p> <p>That process took me about two weeks total. The good news is that I understand WebSocket really well now and I was able to make my own client and server scripts from scratch that work great. Hopefully the culmination of all that information will give you enough guidance and information to code your own WebSocket PHP script. Good luck!</p> <p><strong>Edit</strong>: This edit is a couple of years after my original answer, and while I do still have a working solution, it's not really ready for sharing. Luckily, someone else on GitHub has almost identical code to mine (but much cleaner), so I recommend using the following code for a working PHP WebSocket solution:<br> <a href="https://github.com/ghedipunk/PHP-Websockets/blob/master/websockets.php" rel="noreferrer">https://github.com/ghedipunk/PHP-Websockets/blob/master/websockets.php</a></p> <p><strong>Edit #2</strong>: While I still enjoy using PHP for a lot of server-side related things, I have to admit that I've really warmed up to Node.js a lot recently, and the main reason is because it's better designed from the ground up to handle WebSocket than PHP (or any other server-side language). As such, I've found recently that it's a lot easier to set up both Apache/PHP and Node.js on your server and use Node.js for running the WebSocket server and Apache/PHP for everything else. And in the case where you're on a shared hosting environment in which you can't install/use Node.js for WebSocket, you can use a free service like Heroku to set up a Node.js WebSocket server and make cross-domain requests to it from your server. Just make sure if you do that to set your WebSocket server up to be able to handle cross-origin requests.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload