通过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(); 因为一个TP项目中客户需要全部网页分享支持自定义图片和描述信息,于是自己封装了下 //share()微信分享链接 //参数1 appid //参数2 appsert //参数3 nonceStr随机码 //参数4 timestamp时间戳 public&nb...
/** * 计算两点地理坐标之间的距离 * @param Decimal $longitude1 起点经度 * @param Decimal $lati...
【一】.抽象类假设如下场景:团队准备开发某网站,表建好了,页面设计好了.A组负责开发底层数据库操作类(DB),B组负责调用DB类.但是此时A组发生了争执,MySQL? Oracle? DB2? sqlite?到底使用什么数据库?B组.... 进入漫长的等待.解决方法:A组和B组 先定1个数据库类的模...
项目需要使用websocket推送最新订单,客户服务器非linux不支持swoole,因此使用原生,直接上代码(1).PHP服务端<?php ini_set('error_reporting', E_ALL ^ E_NOTICE); ini_set...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...
php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int { &nb...