socket.io(客户端) 连接失败

yundong

客户端:socket.io version 2.2.1
服务器端: phpsocketio

客户端代码:

var socket = io("$socket_address");
//socket.heartbeatTimeout = 10000;
var room_id = $('#room_id').text();
var room_name = $('#room_name').text();
var username = $('#username').text();
socket.on('connect', function(){
        console.log('connect success');
        socket.emit('subscribe', {'room_id':room_id,'username':username});
        $('#status').text('连接');
        $('#status').addClass('text-success').removeClass('text-danger');
});

服务器端代码:

$io = new SocketIO(8890);

$io->on('workerStart', function() use ($io){
});

$io->on('connection', function($socket) use($io){
    echo "New client connected ".$socket->conn->remoteAddress."\n";
    $socket->on('chat message', function($msg) use($io){
        $io->emit('chat message', $msg);
    });

    $socket->on('subscribe', function($data) use($socket, $io){
        //var_dump($io->sockets->adapter->rooms);
        $room_id = $data;
        $username = $data;
        echo ("$username - Joining room ". $room_id."\n");
        $socket->join("room".$room_id);
    });

    $socket->on('unsubscribe', function($room) use($socket, $io){
        echo ("Leaving room ". $room."\n");
        $socket->leave($room);
    });

    $socket->on('message', function($message) use ($socket, $io){
        echo "Received a message: ".$message."\n";
        $obj = json_decode($message);
        echo "room is ".$obj->room. " ID: ". $obj->room_id."\n";
        //$io->to("room".$obj->room_id)->emit("room".$obj->room_id, $message);
        $io->emit("room".$obj->room_id, $message);
    });

    $socket->on('disconnect', function() use($socket) {
        echo ("client disconnected ".$socket->conn->remoteAddress."\n");
    });
});

Worker::runAll();

结果

client disconnected 118.200.125.242:41557
New client connected 118.200.125.242:41513
yundong - Joining room 53

client disconnected 118.200.125.242:41513
New client connected 118.200.125.242:41513
client disconnected 118.200.125.242:41557

在电脑上没有问题, console里面可以看到 ‘connect success';

但在android的chrome上面,‘connect’有时候激发的比较晚,客户端(socket.emit('subscribe', {'room_id':room_id,'username':username});)没有执行 (socket.connected 打印结果是true)

服务器端log可以看到已连接 “New client connected"

请大家帮我看看 谢谢

8292 0 0
0个回答

年代过于久远,无法发表回答
×
🔝