详细描述:
.现在是运行一个phpsocket.io的demo
启动没问题,是当客户端第一次访问的时候开始报错。
抛出错误的代码路径是vendor/workerman/phpsocket.io/src/Engine/Transports/Polling.php 的14行
部分错误栈:
PHP Notice: Undefined property: PHPSocketIO\Engine\Protocols\Http\Request::$res in ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Transports/Polling.php on line 14
Notice: Undefined property: PHPSocketIO\Engine\Protocols\Http\Request::$res in ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Transports/Polling.php on line 14
exception 'Exception' with message 'empty this->res' in ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Transports/PollingXHR.php:45
Stack trace:
#0 ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Transports/Polling.php(181): PHPSocketIO\Engine\Transports\PollingXHR->doWrite('\x00\x01\x00\x00\xFF0{"sid":"1...')
#1 ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Transports/Polling.php(176): PHPSocketIO\Engine\Transports\Polling->write('\x00\x01\x00\x00\xFF0{"sid":"1...')
#2 ~/work/im/ioim.sscf.com/vendor/workerman/phpsocket.io/src/Engine/Socket.php(312): PHPSocketIO\Engine\Transports\Polling->send(Array)
#3 : PHPSocketIO\Engine\Socket->flush()
尝试:
因为在看代码的时候发现前面的其实是由一个res的,_于是尝试把对应的res赋值给request_;但此时却发现客户端出现另外一个错误。failed: Error during WebSocket handshake: Unexpected response code: 200
修改位置:
vendor/workerman/phpsocket.io/src/Engine/Engine.php 的255行
添加的代码是:
$req->res = $res;
是什么socket.io客户端?js?
是js..
代码:
var socket = io('http://127.0.0.1:8083', {transports:['websocket', 'polling']});
socket.on('connect', function (data) {
console.log(data);
socket.send('hi');
});
直接
var socket = io('http://127.0.0.1:8083');
试下
可以了。。十分感谢。。。
{transports:['websocket', 'polling']}这个参数是以websocket协议为第一优先级,默认是{transports:['polling', 'websocket']}。。我这样的理解有没问题呢?
没仔细研究过socket.io客户端,不知道有这个参数
如果在使用var socket = io('http://127.0.0.1:8083'); 后端代码是没什么问题的。。。也不再存在出现变量不存在的问题了。。