📜 ⬆️ ⬇️

Raise WebSocket server

Many have already heard about the revolution in the expansion of the HTTP protocol - the introduction of WebSocket technology by Google Chromium developers. This article will discuss how to tame this beast - installing and configuring the WebSocket server on the asynchronous phpDaemon framework.

For installation, we need PHP5 CLI> = 5.3 with pcntl, shmop and sockets support.

1. Download


Get the latest version of phpDaemon from the repository
 $ svn checkout http://phpdaemon.googlecode.com/svn/trunk $path/ 

Note that the $ path must be replaced with the path where phpDaemon is installed

2. Installation


Set launch rights
$ chmod +x $path/bin/phpdaemon

Install the libevent library
$ pecl install libevent

We tie the demon to the phpd command
$ ln -s $path/bin/phpdaemon /usr/bin/phpd

')

3. Customization


Configure the $ path / conf / phpdaemon.conf.php file
Copy Source | Copy HTML
  1. <? php
  2. // Turn On Error Display
  3. error_reporting (E_ALL);
  4. ini_set ( 'display_errors' , '1' );
  5. return array (
  6. 'mod-websocketserver-enable' => 1 , // Turn on the web socket server
  7. 'mod-websocketserver-listen' => 'tcp: //0.0.0.0' , // Listen to all IP
  8. 'mod-websocketserver-listenport' => 8047 , // Hang to a different one from 80 so as not to conflict with other web servers
  9. 'max-requests' => 1000 , // Maximum number of requests
  10. 'max-idle' => 0 , // Maximum idle time
  11. 'user' => 'www' , // User
  12. 'group' => 'www' , // and the group under which the daemon runs
  13. 'min-spare-workers' => 5 ,
  14. 'max-spare-workers' => 20 ,
  15. 'start-workers' => 1 ,
  16. 'max-workers' => 50 ,
  17. 'min-workers' => 1 ,
  18. 'path' => dirname ( __FILE__ ). '/appResolver.php'
  19. );
  20. ?>

Create an example of a web socket request handler:

$ path / applications / WebSoketWorker.php
Copy Source | Copy HTML
  1. <? php
  2. return new WebSocketWorker;
  3. / ** <br/> * WebSocket Handler <br/> * <br/> * /
  4. class WebSocketWorker extends AppInstance {
  5. / ** <br/> * WebSocket Server <br/> * @var WebSocketServer <br/> * /
  6. public $ ws ;
  7. / ** <br/> * Initialization handler <br/> * /
  8. public function onReady () {
  9. $ this -> ws = Daemon :: $ appResolver -> getInstanceByAppName ( 'WebSocketServer' );
  10. if ( $ this -> ws) {
  11. $ this -> ws-> routes [ 'myRoute' ] = array ( $ this , 'openSession' );
  12. }
  13. }
  14. / ** <br/> * Opening a new session <br/> * @param $ client <br/> * /
  15. public function openSession ( $ client ) {
  16. return new WebSocketWorkerSession ( $ client );
  17. }
  18. }
  19. / ** <br/> * Websocket Session <br/> * <br/> * /
  20. class WebSocketWorkerSession {
  21. public $ client ;
  22. / ** <br/> * Initialization <br/> * @param $ client <br/> * /
  23. public function __construct ( $ client ) {
  24. Daemon :: log ( 'connected' );
  25. $ this -> client = $ client ;
  26. }
  27. / ** <br/> * Receiving message <br/> * @param $ data - message body <br/> * @param $ type - message type <br/> * /
  28. public function onFrame ( $ data , $ type ) {
  29. Daemon :: log ( $ data );
  30. if ( $ data === 'ping' ) {
  31. $ this -> client-> sendFrame ( 'pong' );
  32. }
  33. }
  34. / ** <br/> * Closing session <br/> * /
  35. public function onFinish () {
  36. }
  37. }
  38. ?>


Voila! Our website server is installed!

4. Testing


For testing, create a file with the following content:

Copy Source | Copy HTML
  1. <script type = "text / javascript" >
  2. <! -
  3. if ( "WebSocket" in window) {
  4. var ws = new WebSocket ( "ws: //127.0.0.1: 8047 / myRoute" );
  5. ws.onopen = function () {
  6. // Web Socket is connected. You can send data using the send () method.
  7. ws.send ( "ping" );
  8. };
  9. // every time the browser gets some data through a web socket
  10. ws.onmessage = function (evt) {
  11. alert ( 'Received message:' + evt.data);
  12. };
  13. ws.onclose = function () {
  14. // websocket closed.
  15. };
  16. } else {
  17. // for browsers that do not support WebSocket.
  18. alert ( 'Your browser does not support websockets' );
  19. }
  20. // ->
  21. </ script>


In case of a successful connection, you should receive a “pong” message.

For support in non- Chrome browsers, use the web-socket-js library, which emulates web sockets using flash.

Source: https://habr.com/ru/post/82140/


All Articles