本文主要給大家介紹了關(guān)于swoole+redis實(shí)現(xiàn)股票和區(qū)塊鏈服務(wù)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
PHP 的redis擴(kuò)展是阻塞式 IO ,使用訂閱/發(fā)布模式時(shí),會導(dǎo)致整個(gè)進(jìn)程進(jìn)入阻塞。因此必須使用Swoole\Redis異步客戶端來實(shí)現(xiàn)。
$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('kline1min'); }); }); $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();
- 在進(jìn)程啟動(onWorkerStart)時(shí)創(chuàng)建了Swoole\Redis客戶端,連接到Redis服務(wù)器
- 連接成功后,訂閱msg_0主題的消息
- 當(dāng)有新的message時(shí),Swoole\Redis會觸發(fā)onMessage事件回調(diào)
- 在這個(gè)回調(diào)函數(shù)中使用$server->connections遍歷服務(wù)器所有的連接,發(fā)送消息
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對的支持。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。