walkor / walkor/web-msg-sender
请问web-msg-sender怎么使用自定义的array?
- Dominant language
- PHP
- Stars
- 927
- Forks
- 404
- PR merge metrics
- No merged PRs in 30d
Description
// 全局数组保存uid在线数据
$uidConnectionMap = array();
$live_agent_arr=array(); //自定义了个函数,在 $socket->on( 'login', function ( $uid )use( $socket ) 引用是null的,不解
echo "is_arraya-".is_array($live_agent_arr)." is_nulla-".is_null($live_agent_arr)." is_stringa-".is_string($live_agent_arr)."\n\n";
// PHPSocketIO服务
$sender_io = new SocketIO( 2120 );
// 客户端发起连接事件时,设置连接socket的各种事件回调
$sender_io->on( 'connection', function ( $socket ) {
// 当客户端发来登录事件时触发
$socket->on( 'login', function ( $uid )use( $socket ) {
global $uidConnectionMap,$last_online_count,$last_online_page_count,$live_agent_arr;
// 已经登录过了
if ( isset( $socket->uid ) ) {
return;
}
// 更新对应uid的在线数据
$uid = ( string )$uid;
if ( !isset( $uidConnectionMap[ $uid ] ) ) {
$uidConnectionMap[ $uid ] = 0;
}
echo "is_arrayb-".is_array($uidConnectionMap)." is_nullb-".is_null($uidConnectionMap)." is_stringb-".is_string($uidConnectionMap)."\n\n"; //执行结果 is_array($uidConnectionMap) 可以 true
echo "is_array-".is_array($live_agent_arr)." is_null-".is_null($live_agent_arr)." is_string-".is_string($live_agent_arr)."\n\n"; //执行结果 is_array($live_agent_arr) 是flase, is_null($live_agent_arr)=true
// 这个uid有++$uidConnectionMap[$uid]个socket连接
++$uidConnectionMap[ $uid ];
if(isset($live_agent_arr)){
array_push( $live_agent_arr, array( "exten" => $uid, "channel" => "" ) );
}
// 将这个连接加入到uid分组,方便针对uid推送数据
$socket->join( $uid );
$socket->uid = $uid;
} );
} );
我十分不理解的是,为何 我自己定义的
$live_agent_arr 在 function ($uid) use($socket) 是 null, 而例子里自带的$uidConntionMap可以正常使用? 我该怎么样使用自己定义的array?
请各位大神不吝赐教,小白跪谢!!!
Contributor guide
No contributing guide indexed for this repository
Research direction
No repository file or test is named. Start with the PHP snippet's SocketIO setup and its connection/login callbacks, then trace how variables declared outside the callbacks are referenced inside them. Done means documenting the supported way to make a custom array available in this handler and clarifying the expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, networking
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100