我是套接字的新手,并使用socket.io在我的应用程序中实现消息传递系统... socket.io在nodejs上正常工作,但是我试图在php中使用它,并使用phpsocket.io事件发射器..它工作正常,我可以发出和接收事件,但是唯一的问题是我无法在套接字处理程序之间共享和设置全局变量。 这给我未定义变量user_id的错误。
phpsocket.io
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
global $user_id; //variable in which i will store some value
$io = new SocketIO(2021);
$io->on('connection', function($socket)use($io){
echo "new Connection connected\n";
$socket->on('join',function($data) use($socket){
$socket->join($data['email']);
echo "room Joined ".$data['email']."\n";
});
$socket->on('chat message',function($data) use($io){
$io->in($data['email'])->emit('chat message',array('msg'=>$data['msg']));
});
$socket->on("online users",function($data)use($socket){
$user_id = $data['userId']; //here i store a value in variable
});
$socket->on('disconnect', function () use($socket) {
echo "disconnected ";
echo $user_id; //here when i try to use the variable it is giving me warning
// of undefined variable
});
});
Worker::runAll();
?>