Home php An example of using a web sockets in php with workerman

An example of using a web sockets in php with workerman

Author

Date

Category

Please give an example of using workerman library. The entire Internet is dotted with examples of the type of the helloworld, which I have on a dozen memory can reproduce itself, but to solve practical problems, they are useless. Sorely lacking on the application of theory.

Did not find any documentation or articles (other than primitive), I read the original Japanese or Chinese with gugltransleytom, download the source code (again, with the characters in the comments), suggested by the author. But it is very difficult to understand the concept, the essence of the library. Perhaps we should examine ourselves more deeply sockets, this answer satisfied me too, but ask again with the specifics, what and where to read.

The problem is quite simple: the client connects to the server, according to what information it needs and starts to wait for messages. The front part is clear, but how the backend must forward the message to the client – does not understand. Where and what to initiate to the data set X, the Y intended customer, came to him.


Answer 1, Authority 100%

Sends a message to a specific user

Server

& lt;? php
  require_once __DIR__. '/Vendor/autoload.php';
  use Workerman \ Worker;
// array to connect the user's connection and we needed parameter
$ Users = [];
// create ws-server that will connect all of our users
$ Ws_worker = new Worker ( "websocket: //0.0.0.0: 8000");
// create a handler that will be executed at startup ws-server
$ Ws_worker- & gt; onWorkerStart = function () use (& amp; $ users)
{
  // create a local tcp-server to send an email notification of the code of our website
  $ Inner_tcp_worker = new Worker ( "tcp: //127.0.0.1: 1234");
  // create a message handler, which will be triggered,
  // when the local tcp-socket receives a message
  $ Inner_tcp_worker- & gt; onMessage = function ($ connection, $ data) use (& amp; $ users) {
    $ Data = json_decode ($ data);
    // send messages to users on the userId
    if (isset ($ users [$ data- & gt; user])) {
      $ Webconnection = $ users [$ data- & gt; user];
      $ Webconnection- & gt; send ($ data- & gt; message);
    }
  };
  $ Inner_tcp_worker- & gt; listen ();
};
$ Ws_worker- & gt; onConnect = function ($ connection) use (& amp; $ users)
{
  $ Connection- & gt; onWebSocketConnect = function ($ connection) use (& amp; $ users)
  {
    // when a new user saves a get-a parameter that yourself and transferred from the page Site
    $ Users [$ _ GET [ 'user']] = $ connection;
    // instead of get-parameter you can also use the option of the cookie, for example $ _COOKIE [ 'PHPSESSID']
  };
};
$ Ws_worker- & gt; onClose = function ($ connection) use (& amp; $ users)
{
  // delete the setting when the user logs off
  $ User = array_search ($ connection, $ users);
  unset ($ users [$ user]);
};
// Run worker
Worker :: runAll ();

Client

& lt;! doctype HTML & GT;
& Lt; html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en" lang = "en" & gt;
& lt; Head & gt;
  & lt; script & gt;
    ws = new WebSocket ( "ws: //127.0.0.1: 8000 / user = tester01?");
    ws.onmessage = function (evt) {alert (evt.data);};
  & lt; / script & gt;
& lt; / Head & gt;
& lt; / html & gt;

Sends a message to

& lt;? php
$ Localsocket = 'tcp: //127.0.0.1: 1234';
$ User = 'tester01';
$ Message = 'test';
// connect to a local tcp-server
$ Instance = stream_socket_client ($ localsocket);
// send message
fwrite ($ instance, json_encode ([ 'user' = & gt; $ user, 'message' = & gt; $ message]) "\ n".);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions