客户端连接时phpsocket.io报错,walkor老大进!

kwrong

客户端代码:

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>JS连接websock测试</title>
<script src='//cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js'></script>
<script src='//cdn.bootcss.com/jquery/1.11.3/jquery.js'></script>
</head>
<body bgcolor="#bfd">
<h3>消息容器、</h3>
<i>轮询重连次数</i>
<br><b>连接状态</b>
<script type="text/javascript">
var uid = 233;
$(document).ready(function () {
    // var socket = io('http://127.0.0.1:2120');
    var socket = io('http://127.0.0.1:2120',{
                  transports: //, autoConnect: false
                });
    var reconnectimes = 0;  //连接次数,一打开就触发了一次不算
    socket.on('reconnect_attempt', () => {
      socket.io.opts.transports = ;
      reconnectimes++;
      $("i").html('尝试自动重连第'+reconnectimes+'次<br>');
    });
    socket.on('disconnect', () => {
      //socket.open(); //手动连接,非自动重连的模式下使用
      $("b").html('连接状态:断开');
    });
    // 连接后登录
    socket.on('connect', function(){
        socket.emit('login', uid);
        $("b").html('连接状态:已连接');
    });
    // 后端推送来消息时
    socket.on('new_msg', function(msg){
        $("h3").html('收到消息:'+msg);
    });
    // 后端推送来在线数据时
    socket.on('update_online_count', function(online_stat){
        $('#online_box').html(online_stat);
    });
});
</script>
</body>
</html>

CMD报错信息:

Workerman version:3.5.1          PHP version:7.0.10
------------------------ WORKERS -------------------------------
worker        listen                   processes status
WebServer     http://0.0.0.0:2123       1        
PHPSocketIO   SocketIO://0.0.0.0:2120   1        
----------------------------------------------------------------
Press Ctrl-C to quit. Start success.

Warning: Missing argument 4 for PHPSocketIO\Engine\Engine::dealWebSocketConnect(
) in D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\
Engine.php on line 264

Notice: Undefined property: PHPSocketIO\Engine\Protocols\Http\Request::$res in D
:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Transp
orts\Polling.php on line 14
Exception: empty this->res in D:\qampp\htdocs\msgsender\vendor\workerman\phpsock
et.io-for-win\src\Engine\Transports\PollingXHR.php:45
Stack trace:
#0 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Tr
ansports\Polling.php(181): PHPSocketIO\Engine\Transports\PollingXHR->doWrite('\x
00\x01\x00\x01\xFF0{"sid":"3...')
#1 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Tr
ansports\Polling.php(176): PHPSocketIO\Engine\Transports\Polling->write('\x00\x0
1\x00\x01\xFF0{"sid":"3...')
#2 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\So
cket.php(318): PHPSocketIO\Engine\Transports\Polling->send(Array)
#3 : PHPSocketIO\Engine\Socket->flush()
#4 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Event\Emi
tter.php(91): call_user_func_array(Array, Array)
#5 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Tr
ansports\Polling.php(50): PHPSocketIO\Event\Emitter->emit('drain')
#6 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Tr
ansports\Polling.php(18): PHPSocketIO\Engine\Transports\Polling->onPollRequest(O
bject(PHPSocketIO\Engine\Protocols\Http\Request), NULL)
#7 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\Tr
ansports\PollingXHR.php(28): PHPSocketIO\Engine\Transports\Polling->onRequest(Ob
ject(PHPSocketIO\Engine\Protocols\Http\Request))
#8 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\En
gine.php(212): PHPSocketIO\Engine\Transports\PollingXHR->onRequest(Object(PHPSoc
ketIO\Engine\Protocols\Http\Request))
#9 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\En
gine.php(296): PHPSocketIO\Engine\Engine->handshake(Object(PHPSocketIO\Engine\Tr
ansports\PollingXHR), Object(PHPSocketIO\Engine\Protocols\Http\Request))
#10 : PHPSocketIO\Engine\Engine->dealWebSocketConnect(NULL, t
rue, Object(PHPSocketIO\Engine\Protocols\Http\Request))
#11 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\E
ngine.php(142): call_user_func(Array, NULL, true, Object(PHPSocketIO\Engine\Prot
ocols\Http\Request))
#12 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\E
ngine.php(133): PHPSocketIO\Engine\Engine->checkRequest(Object(PHPSocketIO\Engin
e\Protocols\Http\Request), Array)
#13 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\E
ngine.php(261): PHPSocketIO\Engine\Engine->verify(Object(PHPSocketIO\Engine\Prot
ocols\Http\Request), Object(PHPSocketIO\Engine\Protocols\Http\Response), true, A
rray)
#14 : PHPSocketIO\Engine\Engine->onWebSocketConnect(Object(Wo
rkerman\Connection\TcpConnection), Object(PHPSocketIO\Engine\Protocols\Http\Requ
est), Object(PHPSocketIO\Engine\Protocols\Http\Response))
#15 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\P
rotocols\WebSocket\RFC6455.php(283): call_user_func_array(Array, Array)
#16 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\P
rotocols\WebSocket.php(84): PHPSocketIO\Engine\Protocols\WebSocket\RFC6455::deal
Handshake(Object(Workerman\Connection\TcpConnection), Object(PHPSocketIO\Engine\
Protocols\Http\Request), Object(PHPSocketIO\Engine\Protocols\Http\Response))
#17 D:\qampp\htdocs\msgsender\vendor\workerman\phpsocket.io-for-win\src\Engine\P
rotocols\SocketIO.php(38): PHPSocketIO\Engine\Protocols\WebSocket::dealHandshake
(Object(Workerman\Connection\TcpConnection), Object(PHPSocketIO\Engine\Protocols
\Http\Request), Object(PHPSocketIO\Engine\Protocols\Http\Response))
#18 D:\qampp\htdocs\msgsender\vendor\workerman\workerman-for-win\Connection\TcpC
onnection.php(593): PHPSocketIO\Engine\Protocols\SocketIO::input('GET /socket.io
/...', Object(Workerman\Connection\TcpConnection))
#19 : Workerman\Connection\TcpConnection->baseRead(Resource i
d #33)
#20 D:\qampp\htdocs\msgsender\vendor\workerman\workerman-for-win\Events\Select.p
hp(286): call_user_func_array(Array, Array)
#21 D:\qampp\htdocs\msgsender\vendor\workerman\workerman-for-win\Worker.php(856)
: Workerman\Events\Select->loop()
#22 D:\qampp\htdocs\msgsender\vendor\workerman\workerman-for-win\Worker.php(473)
: Workerman\Worker->run()
#23 D:\qampp\htdocs\msgsender\vendor\workerman\workerman-for-win\Worker.php(382)
: Workerman\Worker::runAllWorkers()
#24 D:\qampp\htdocs\msgsender\start_io.php(107): Workerman\Worker::runAll()
#25 {main}
5029 2 0
2个回答

walkor 打赏
    var socket = io('http://127.0.0.1:2120',{
          transports: //, autoConnect: false
    });

phpsocket.io不支持 设置transports。
当初写phpsocket.io时没想到有这个用法。

  • kwrong 2017-09-14

    哦,我还以为phpsocket.io是walkor老大写的呢

  • walkor 2017-09-15

    额,是我写的

kwrong

其实,这样子就能判断所有状态了,谢谢walkor

<script type="text/javascript">
var uid = 233;
$(document).ready(function () {
    // 连接服务端
    var socket = io('http://127.0.0.1:2120');
    var reconnectimes = 0;  //连接次数,一打开就触发了一次不算
    socket.on('reconnect_attempt', () => {
      reconnectimes++;
      $("i").html('尝试自动重连第'+reconnectimes+'次<br>');
    });
    socket.on('disconnect', (reason) => {
      //socket.open(); //手动连接,非自动重连的模式下使用
      $("b").html('连接状态:断开');
    });

    // 连接后登录
    socket.on('connect', function(){
        socket.emit('login', uid);
        $("b").html('连接状态:已连接');
    });
    // 后端推送来消息时
    socket.on('new_msg', function(msg){
        $("h3").html('收到消息:'+msg);
    });
    // 后端推送来在线数据时
    socket.on('update_online_count', function(online_stat){
        $('#online_box').html(online_stat);
    });
});
</script>
  • 暂无评论
年代过于久远,无法发表回答
×
🔝