RatchetPHP 没有用于新连接的 WebSocket 属性

RatchetPHP no WebSocket property for new connections

我正在尝试使用 Ratchet 在 Websocket 服务器的 onOpen 函数中访问传入连接的查询参数。两个 official documentation and other Whosebug 帖子都说您可以通过访问传递给函数的 ConnectionInterface 对象的 WebSocket 属性 来做到这一点:

public function onOpen(ConnectionInterface $conn) {
    $query = $conn->WebSocket->request->getQuery();
}

但是,传入连接对象没有 WebSocket 属性。当我启动此服务器并与客户端连接时,会发出通知,这会导致在 null 对象上调用函数时出现致命错误:

PHP Notice: Undefined property: Ratchet\Server\IoConnection::$WebSocket

我正在使用 PHP 7.0,我需要在我的 composer.json 中安装最新的稳定版本:

"require": {
    "cboden/ratchet": "^0.3.6"
}

我在 Chrome JS 控制台中连接客户端,代码也直接从 Hello World 文档中复制:

var conn = new WebSocket('ws://localhost:8080?foo=bar');
conn.onopen = function(e) {
    console.log("Connection established!");
};

如果有帮助,我看到的 $conn 的唯一 public 属性是:

bufferSize
stream
readable
writable
closing
loop
buffer
listenerse
decor

我遇到了同样的问题,我发现:

 $querystring = $conn->httpRequest->getUri()->getQuery();
 parse_str($querystring,$queryarray);

允许您访问查询参数。