3 Star 0 Fork 0

positvmans/socket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bluetooth.php 4.99 KB
一键复制 编辑 原始数据 按行查看 历史
positvmans 提交于 2017-05-23 19:14 +08:00 . socket
<?php
/**
* Created by PhpStorm.
* User: fanweifeng
* Date: 16/12/13
* Time: 16:27
*/
use Workerman\Worker;
use Workerman\WebServer;
use Workerman\Lib\Timer;
use PHPSocketIO\SocketIO;
include __DIR__ . '/vendor/autoload.php';
// 全局数组保存loginId在线数据
$uidConnectionMap = array();
// 记录最后一次广播的在线用户数
$last_online_count = 0;
// 记录最后一次广播的在线页面数
$last_online_page_count = 0;
// PHPSocketIO服务
$sender_io = new SocketIO(2122);
// 客户端发起连接事件时,设置连接socket的各种事件回调
$sender_io->on('connection', function($socket){
// 当客户端发来登录事件时触发
$socket->on('login', function ($data)use($socket){
// @file_put_contents('/tmp/blue.txt',$data,FILE_APPEND);
global $uidConnectionMap, $last_online_count, $last_online_page_count;
// $data_json = array('loginId'=>123);
// $uid = $data_json['loginId'];
// // 已经登录过了
// if(isset($socket->uid)){
// return;
// }
// // 更新对应uid的在线数据
// //$uid = (string)$uid;
// if(!isset($uidConnectionMap[$uid]))
// {
// $uidConnectionMap[$uid] = 0;
// }
// // 这个uid有++$uidConnectionMap[$uid]个socket连接
// ++$uidConnectionMap[$uid];
// // 将这个连接加入到uid分组,方便针对uid推送数据
// $socket->join($uid);
// $socket->uid = $uid;
$result_json = get_data($data);
// 更新这个socket对应页面的在线数据
$socket->emit('update_online_count', $result_json);
});
$socket->on('wave_done', function ($data)use($socket){
$result_json = get_data($data);
$socket->emit('update_online_count', $result_json);
});
$socket->on('touch_done', function ($data)use($socket){
//@file_put_contents('/Users/fanweifeng/Documents/blue_array_wave.txt',$data,FILE_APPEND);
$result_json = get_data($data);
//$socket->emit('update_online_count', $id);
});
// 当客户端断开连接是触发(一般是关闭网页或者跳转刷新导致)
$socket->on('disconnect', function () use($socket) {
if(!isset($socket->uid))
{
return;
}
global $uidConnectionMap, $sender_io;
// 将uid的在线socket数减一
if(--$uidConnectionMap[$socket->uid] <= 0)
{
unset($uidConnectionMap[$socket->uid]);
}
});
});
// 当$sender_io启动后监听一个tcp端口,通过这个端口可以给任意uid或者所有uid推送数据
$sender_io->on('workerStart', function(){
// 监听一个tcp端口
$inner_http_worker = new Worker('tcp://0.0.0.0:2123');
// 当tcp客户端发来数据时触发
$inner_http_worker->onMessage = function($http_connection, $data){
global $uidConnectionMap;
$result_json_data = json_decode($data,true);
global $sender_io;
$sender_io->emit('new_msg', $data);
//分别通过loginId推送数据
// if($result_json_data['flag']) {
// $sender_io->emit('new_msg', $data);
// }
// @file_put_contents('/Users/fanweifeng/Documents/test.txt',$data,FILE_APPEND);
// http接口返回,如果用户离线socket返回fail
// if(!empty($result_json_data['loginId']) && !isset($uidConnectionMap[$result_json_data['loginId']])){
// return $http_connection->send('offline');
// }else{
// return $http_connection->send('ok');
// }
return $http_connection->send('fail');
};
// 执行监听
$inner_http_worker->listen();
});
/*function get_data($uid){
//$url = 'http://localhost/web-msg-sender/test.php';
//@file_put_contents('/Users/fanweifeng/Documents/curl.txt',$uid,FILE_APPEND);
$url = 'http://test-new-dtq.qinxue100.com/game/bluetooth/socket_client';
$post_data = json_decode($uid,true);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
$return = curl_exec ( $ch );
curl_close ( $ch );
return $return;
}*/
function get_data($data){
// @file_put_contents('/tmp/zpp.txt',$data,FILE_APPEND);
$url = "http://test-dtq2.qinxue100.com/game/bluetooth/socket_client";
//$url = "http://dev-dtq.qinxue100.com/game/bluetooth/socket_client";
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
return $result;
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/project_fwf/socket.git
git@gitee.com:project_fwf/socket.git
project_fwf
socket
socket
master

搜索帮助