In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
PHP's redis extension is a blocking IO, and when using the subscription / publish mode, it will cause the entire process to enter blocking. Therefore, it must be implemented using the Swoole\ Redis asynchronous client.
Instance code
$server = new swoole_websocket_server ("0.0.0.0", 9501); $server- > on ('workerStart', function ($server, $workerId) {$client = new swoole_redis; $client- > on (' message', function (swoole_redis $client, $result) use ($server) {if ($result [0] = = 'message') {foreach ($server- > connections as $fd) {$server- > push ($fd, $result [1]) }); $client- > connect ('127.0.0.1, 6379, function (swoole_redis $client, $result) {$client- > subscribe (' msg_0');}); $server- > on ('open', function ($server, $request) {}); $server- > on (' message', function (swoole_websocket_server $server, $frame) {$server- > push ($frame- > fd, "hello");}) $server- > on ('close', function ($serv, $fd) {}); $server- > start ()
Realization process
The Swoole\ Redis client is created when the process starts (onWorkerStart) to connect to the Redis server
After the connection is successful, subscribe to the msg_0 topic message
When there is a new message, Swoole\ Redis triggers the onMessage event callback
In this callback function, use $server- > connections to traverse all connections to the server and send messages
These are the details of forwarding subscription messages from Redis to WebSocket clients. For more information, please follow other related articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.