您的位置:首页 > 数据库 > Redis

利用swoole+redis实现股票和区块链服务

2017-09-10 15:39 489 查看

本文主要给大家介绍了关于swoole+redis实现股票和区块链服务的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

PHP 的redis扩展是阻塞式 IO ,使用订阅/发布模式时,会导致整个进程进入阻塞。因此必须使用Swoole\Redis异步客户端来实现。

$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();
  • 在进程启动(onWorkerStart)时创建了Swoole\Redis客户端,连接到Redis服务器
  • 连接成功后,订阅msg_0主题的消息
  • 当有新的message时,Swoole\Redis会触发onMessage事件回调
  • 在这个回调函数中使用$server->connections遍历服务器所有的连接,发送消息

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐