当前位置:首页 > PHP > 正文内容

php使用swoole扩展推送消息

高老师9年前 (2017-12-29)PHP3019

通过http推送消息给socket,socket服务再向客户端推送

<?php
/*
 * Socket推送
 * 请用守护进程方式启动php msgservice.php &   (socket只支持linux)
 * 默认使用SWOOLE_PROCESS模式,echo终端是禁止的,如果要观察日志,保存到log中.
 * status=1,小程序推送
 * status=2,点餐机->socket->身份识别    
 * status=3,socket->点餐机->推送订单
 * status=4,点餐机->socket->告知消息已经处理
 */
class WebsocketServer {
    public $server;
	
	//主机群
	private $client=array();	
	//消息群
	private $clientmsg=array();
	
    public function __construct() {
    	//01.绑定网卡
        $this->server = new swoole_websocket_server("0.0.0.0", 9502);
		
		//02.主动握手
        $this->server->on('open', function (swoole_websocket_server $server, $request) {
                //echo "server: handshake success with fd{$request->fd}\n";
            });
		//03.消息获取
        $this->server->on('message', function (swoole_websocket_server $server, $frame) {
        	
                //echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
				
				//3.1.记录客户端信息
				$data=json_decode($frame->data,true);

				if(isset($data['status']) && $data['status']==2){
					$rev=array(
						'hostfd'=>$frame->fd,//'在swoole中是顺序'
						'shopid'=>$data['shopid'],
					);
					
					//新来的客户端,看看是否有你的订单
					foreach ($this->clientmsg as  $value) {
						if($value['shopid']==$rev['shopid']){
							//echo 'have';
							$data=json_encode($value);
							$server->push($frame->fd,$data);
							break;
						}
					}
					//多个终端只识别第一个
					$isnew=1;
					foreach ($this->client as  $value) {
						if($value['shopid']==$rev['shopid']){
							$isnew=0;
							break;
						}
					}
					if($isnew){
						array_push($this->client,$rev);						
					}
					else
					{
						//echo 'This client duplication,now client num:'.count($this->client).'\n';
					}
					
				}
				
				//3.2.点餐机已经处理的消息直接清理
				if( isset($data['status']) && $data['status']==3){
					$orderid=$data['orderid'];
					foreach ($this->clientmsg as $key => $value) {
							if($value['orderid']==$orderid){
								unset($this->clientmsg[$key]);
								array_merge($this->clientmsg);
								//echo 'order  is  reved!';
								break;
							}
					}
				}				
            });
		//04.断开事件,清理无效主机
        $this->server->on('close', function ($ser, $fd) {
				foreach ($this->client as $key => $value) {
						if($value['hostfd']==$fd){
							unset($this->client[$key]);
							array_merge($this->client);
						}
				}
				//echo "client {$fd} closed,now client num:".count($this->client).'\n';
            });
		//05.小程序推送,实例不走nginx/apache,请访问本进程端口	
        $this->server->on('request', function ($request, $response) {

        		//带有推送表示的主体
        		if(isset($request->post['status']) && $request->post['status']==1)
        		{
        			//接收小程序推送	
        			$rev=array(
        				'type'=> $request->post['type'],
        				'shopid' =>$request->post['shopid'],
 						'orderid'=>$request->post['orderid'],
					);
					
					$response->end('ok');

					//直接尝试发送给点餐机
					foreach ($this->client as $value) {
						if($value['shopid']==$rev['shopid']){
							$data=json_encode($rev);
							$this->server->push($value['hostfd'],$data);
							break;
						}
					}
					//入列
					array_push($this->clientmsg,$rev);
					
        		}

        });
			
        $this->server->start();
    }
	
}
new WebsocketServer();

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/53.html

分享给朋友:

“php使用swoole扩展推送消息” 的相关文章

PHP高并发下数据库值更新的问题

PHP高并发下数据库值更新的问题

(1).创建数据库test ,创建表shop(字段id,total),商品id是1,商品总数10    (2).PHP模拟购买,商品数量大于0才能购买<?php //连接数据库 $con=mysqli_connect("192.168.2.18...

php soap 捕获异常,使用try catch 捕获Soap 异常

php soap 捕获异常,使用try catch 捕获Soap 异常

项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try {     $url = 'http...

php  while  true  cpu占用100%

php while true cpu占用100%

在编写多进程的实例中我在每个进程中使用如下代码://调用等待信号的处理器 while (true) {     pcntl_signal_dispatch(); }开启5个进程,cpu直接100%修正之后的代码://调用等待信号的处理器 while&...

php event异常处理,php set_exception_handler无效

php event异常处理,php set_exception_handler无效

(1).今天遇到一件奇怪的事情,在event事件中是无法自定义异常处理,例如我们使用set_exception_handler来统一处理异常。例如下面的代码:<?php error_reporting(E_ALL); set_error_handler(function ($errn...

php curl Received HTTP code 403 from proxy after CONNECT

php curl Received HTTP code 403 from proxy after CONNECT

在调用微信code换openid的接口curl报错curl Received HTTP code 403 from proxy after CONNECT,错误码56。可以看到是curl的代理有问题。然后我自己电脑设置代理去访问curl请求的地址,的确也返回了403,说明代理不允许访问这个地址,联系...

php pecl设置代理,php pear设置代理

php pecl设置代理,php pear设置代理

pear config-set http_proxy 47.94.200.124:3128 pecl config-set http_proxy 60.216.101.46:32868家里的长城宽带什么都上不去,安装php扩展真麻烦,使用网...