wh33t Posted May 23, 2017 Share Posted May 23, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303992-will-this-work-as-a-websocket-server/ Share on other sites More sharing options...
Solution kicken Posted May 23, 2017 Solution Share Posted May 23, 2017 (edited) 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. Edited May 23, 2017 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/303992-will-this-work-as-a-websocket-server/#findComment-1546755 Share on other sites More sharing options...
wh33t Posted May 23, 2017 Author Share Posted May 23, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303992-will-this-work-as-a-websocket-server/#findComment-1546774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.