search

Home  >  Q&A  >  body text

php - Ask a question about swoole websocket

Swoole’s websocket will be used in the development in the past two days, but I don’t understand some of it

$serv = new swoole_websocket_server("127.0.0.1", 9502);

$serv->on('Open', function($server, $req) {
    echo "connection open: ".$req->fd;
});

$serv->on('Message', function($server, $frame) {
    echo "message: ".$frame->data;
    $server->push($frame->fd, json_encode(["hello", "world"]));
});

$serv->on('Close', function($server, $fd) {
    echo "connection close: ".$fd;
});

$serv->start();

This is the demo of swoole official website. When the client sends a message to the server by listening to the message event, the server will send the message to the client. If I want to realize this scenario: if there is a message in the queue, then Send a message to the user (the queue here is assumed to be a redis queue). In this case, the message event cannot be monitored. How can this be achieved?

怪我咯怪我咯2867 days ago531

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-16 13:06:52

    Then you have to store the data of online users. . After connecting, record the user ID and the client's ID in the websocket, one-to-one correspondence. If there is data, you call the push method to push the data to the user

    reply
    0
  • Cancelreply