php - Ask a question about swoole websocket
怪我咯
怪我咯 2017-05-16 13:04:52
0
1
499

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?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
PHPzhong

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template