注意:需要php8.0以上版本,否則需要把php8以上的函數(shù)和代碼改掉
https://gitee.com/windthesky/php-socket.io-gateway-worker
V2、V3、V4 都支持 ,也可以混合使用,混合使用可以參考demo,把socket.io打開或者注釋掉即可
<script src='socket.io-4.7.5.js'></script> <!--<script src='socket.io-3.1.3.js'></script>--> <!--<script src='socket.io-2.3.0.js'></script>-->
和GatewayWorker使用方法基本一樣
Linux: php start.php start
windows:雙擊start_for_win.bat
注意:這個和GatewayWorker不一樣,因為Events已用于socket.io消息處理
#是否允許日記輸出
APP_DEBUG=true
#是否允許跨域
ALLOW_CORS=true
#消息模式,1:由Events.php處理,2:由IoEvents.php處理,3:共同處理
MSG_HANDLE_MODE=1
#需要登錄
NEED_LOGIN=true
#登錄調(diào)用方法
LOGIN_FUNCTION=login
#保存在session中的tag來驗證是否登錄,設置為true是已登錄
LOGIN_TAG=auth
#主要配置
GATEWAY=SocketIO://0.0.0.0:11111
REGISTER=text://0.0.0.0:11112
REGISTER_ADDRESS=127.0.0.1:11112
#網(wǎng)關配置
GATEWAY_NAME=SocketIO
GATEWAY_COUNT=4
GATEWAY_PING_INTERVAL=30
GATEWAY_PING_INTERVAL_EIO4=20
GATEWAY_PING_NOT_RESPONSE_LIMIT=1
GATEWAY_START_PORT=11115
GATEWAY_LAN_IP=127.0.0.1
#業(yè)務進程配置
WORKER_NAME=業(yè)務進程
WORKER_COUNT=16
#使用共享變量,目前只有服務器端的ack用到,不使用服務器端的ack可關閉
USE_GLOBALDATA=true
#變量共享組件
GLOBALDATA_HOST=127.0.0.1
GLOBALDATA_PORT=11113
GLOBALDATA_ADDRESS=127.0.0.1:11113
一個socket.io事件對應一個靜態(tài)方法
/** @noinspection PhpUnused */
use GatewayWorker\Lib\Gateway;
use Workerman\Timer;
/**
* 無特殊情況,只用關注本文件即可
* 不需要的方法都可以刪除
* 可以使用GatewayWorker的所有功能
* 發(fā)送消息,需使用emit_msg編碼消息發(fā)送,請參考示例
*/
class IoEvents
{
public static function onWorkerStart($businessWorker): void
{
try {
if($businessWorker->id===0){
global $global_data;
$global_data->group_list=create_group_list();
Timer::add(10, function(){
Gateway::sendToAll(emit_msg(
'online_count',
Gateway::getAllClientIdCount()
));
});
}
} catch (Throwable $e) {
write_log('IoEvents-onWorkerStart:異常==》'.$e->getMessage(),'錯誤');
}
}
public static function onConnect(int|string $client_id): void
{
// write_log('IoEvents-連接==》'.$client_id);
try {
$_SESSION['auth']=false;
// 連接到來后,定時30秒關閉這個鏈接,需要30秒內(nèi)發(fā)認證并刪除定時器阻止關閉連接的執(zhí)行
$_SESSION['auth_timer_id'] = Timer::add(30, function($client_id){
$session=Gateway::getSession($client_id);
if(empty($session)) return;
if(empty($session['auth'])){
write_log('IoEvents連接:監(jiān)測到未登錄,斷開==》'.$client_id);
Gateway::closeClient($client_id);
}else{
write_log('IoEvents連接:登錄成功==》'.$client_id);
}
}, array($client_id), false);
// write_log('IoEvents連接:完成==》'.$client_id);
} catch (Throwable $e) {
write_log('IoEvents連接:異常==》'.$client_id.',異常內(nèi)容==》'.$e->getMessage(),'錯誤');
}
}
public static function login(int|string $client_id,$msg): void
{
write_log('IoEvents-login==》'.$client_id.json_encode_cn($msg));
Gateway::bindUid($client_id, $msg['id']);
$_SESSION['auth']=true;
$_SESSION['user_info']=$msg;
Timer::del($_SESSION['auth_timer_id']);
global $global_data;
$group_list=$global_data->group_list;
foreach ($group_list as $v){
Gateway::joinGroup($client_id, $v['id']);
}
$user_list = array_merge([$msg], $group_list);
Gateway::sendToCurrentClient(emit_msg(
'login_success',$user_list
));
}
public static function send_msg(int|string $client_id,$msg): void
{
write_log('IoEvents-send_msg==》'.json_encode_cn($msg));
if($msg['user_type']===1){
Gateway::sendToUid(
$msg['receive_id'],
emit_msg('send_msg',$msg)
);
}else{
Gateway::sendToGroup(
$msg['receive_id'],
emit_msg('send_msg',$msg),
[$client_id]
);
}
}
public static function exit_group(int|string $client_id,$msg): void
{
Gateway::leaveGroup($client_id, $msg['id']);
}
public static function onClose(int|string $client_id): void
{
try {
// write_log('IoEvents-離開==》'.$client_id);
Timer::del($_SESSION['auth_timer_id']);
} catch (Throwable $e) {
write_log('IoEvents斷開連接:異常==》'.$client_id.',異常內(nèi)容==》'.$e->getMessage(),'錯誤');
}
}
public static function ferret(int|string $client_id,$ack,$msg2): void
{
Gateway::sendToCurrentClient(emit_msg_ack_res(
$ack,[
'client_id'=>$client_id,
'ss'=>'wood'
]
));
Gateway::sendToCurrentClient(emit_msg_ack(
'hello',
'abc',
[
'client_id'=>$client_id,
'kk'=>'wood'
]
));
}
public static function abc(int|string $client_id,...$msg): void
{
write_log('IoEvents-abc==》'.json_encode_cn($msg));
}
public static function HandleAck(int|string $client_id,int|string $ack_sn,...$msg): void
{
write_log('IoEvents-HandleAck==》'.json_encode_cn($msg));
}
}
比如判斷連接成功后登錄
需要調(diào)用emit_msg發(fā)送消息,emit_msg方法參考如下
第一個參數(shù)是事件名稱,第二個參數(shù)是消息內(nèi)容,消息內(nèi)容可以是數(shù)組,也可以是字符串,數(shù)字等Gateway::sendToCurrentClient(emit_msg( 'connect_success',[ 'client_id'=>$client_id, 'sid'=>$_SESSION['sid'] ] ));
請參考demo中的index.html文件
支持