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

yipeiwu_com5年前PHP代码库

本文主要给大家介绍了关于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遍历服务器所有的连接,发送消息

总结

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

相关文章

php实现的通用图片处理类

本文实例讲述了php实现的通用图片处理类。分享给大家供大家参考。具体如下: 该图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色,处理并保存历史记录的思路:当有图片...

php获取域名的google收录示例

复制代码 代码如下: function get_index($domain){ $url="http://www.google.com/search?source=hp&biw=1440...

php 魔术方法使用说明

PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!!1.__construct(...

PHP文件锁定写入实例解析

本文以实例讲述了PHP文件写入方法,以应对多线程写入,具体代码如下: function file_write($file_name, $text, $mode='a', $timeo...

php通过COM类调用组件的实现代码

在PHP 4.2.0 至 4.2.3中,可以使用w32api_register_function 函数调用外部的DLL,前提是需要在php.ini中打开扩展的php_w32api.dll...