通过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(); <?php function go($str) { echo'I\'m '.$str; } $goto='go'; $goto('gaojiufeng'...
上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...
前面的文章对于高并发下单商品导致商品库存为负值的问题请先阅读再阅读本篇文章一定对您有帮助,建议亲手测试较好。加上文件锁后的下单处理代码:【一】.阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行)<?php //连接数据库 $con=mysqli_connect(...
001源码:/* * $xml_str是xml字符串 */ function xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...
最近在公司开发一个新的项目假设项目域名是a.com,需要接入b.com的单点登陆系统。(1).首先我们会在a.com的登陆页面用iframe引入b.com来显示登陆界面,实际上登陆验证操作都是在b.com上面(2).当b.com验证通过,会在前端ajax请求a.com的回调地址,这个回调地址目的就是...
最近在编写windows php多线程的东西,从官网下载了PHP的线程安全版,尝试开启curl扩展extension=php_curl.dllphp -m 却提示 PHP Startup: Unable to load dynamic library 'C:\php\ext\php_curl...