您的位置 首页 php

php实时推送系统消息给客户端的原理及详解

php实时推送系统消息给客户端的原理及详解

在我们实际开发过程中,有些数据需要实时获取;

比如erp系统中的订单信息,OA系统中的流程审批等都需要及时处理,这时我们就不能再使用http协议了;当然也可以使用轮询的机制。

但是轮询请求中有大半是无用,浪费带宽和服务器资源。

这时我们就得使用websocket协议来满足这种业务需求;

准备工作:

安装PHP-swoole拓展;

直接贴代码;

<?phpnew class{    private $_serv = null;    public function __construct()    {        $this->_serv = new swoole_websocket_server('0.0.0.0', 6552);        $this->_serv->set(array(            'worker_num'      => 2,            'dispatch_mode'   => 3,        'log_file' => 'swoole.log',        ));  //增加个监听端口    $udpworker =  $this->_serv->listen("127.0.0.1", 6553, SWOOLE_SOCK_UDP);    $udpworker->on('Packet', function ($serv, $data, $addr) {            $data = json_decode($data, true);            if(!empty($data)){                //你的业务逻辑            }        });        $this->_serv->on('open', array($this, 'onStart'));        $this->_serv->on('message', array($this, 'onMessage'));        $this->_serv->on('close', array($this, 'onClose'));        $this->_serv->start();    }       public function onStart($serv, $request)    {        echo "server: connect success with fd {$request->fd}n";    }    //format:'{"school_class_id":"1","school_id":"2"}'    public function onMessage($serv, $frame)    {    /**start*你的业务逻辑***/    }    public function onClose($serv, $fd)    {        echo "client {$fd} closedn";    }}?>

原理:

首先创建websocket服务器对象,监听0.0.0.0:6552端口,然后在用服务对象去监听UDP 6553的端口。客户端消息发送到6553借口,然后在通过6552端口发送给用户。

更多相关php知识,请访问php教程!

以上就是php实时推送系统消息给客户端的原理及详解的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:php实时推送系统消息给客户端的原理及详解

文章地址:https://www.zhihuclub.com/2066.shtml

关于作者: 智云科技

热门文章

评论已关闭

5条评论

  1. Reliable Phyrmacy Codes quaniubs – Cialis rasiappepe generic cialis vs brand cialis reviews Clourl Irobbyrode Discount Propecia Without A Prescription

  2. Buy Diflucan Single Dose Eagefmig – Cialis bladeoleve Viagra Flussig Nebenwirkungen Rownhighneew VopWhors Levitra Long Term Effects

  3. 5mg and tadalafil 5mg known as Tadalafil Daily are more ideal for those who have sex frequently, such as those who are in long term relationships

  4. Our online assessment will help determine if you are a good candidate for a medication refill A physician will ultimately determine whether the medication refill is appropriate A prescription for up to a 3 month supply of your medication can be sent to the pharmacy of your choice Our medical team will request current lab results or the completion of new lab tests after a period of about 9-12 months from your first refill visit.

  5. In my experience, the use of ultrasound has largely negated the need to perform a diagnostic thoracocentesis, and limits unnecessary discomfort and eliminates the possibility of iatrogenic pneumothorax In addition, different genes may be responsible for the same condition e

网站地图