I’m trying to create websocket server in yii2, when I start the server from command php yii server/start it says server started on port 81 but as I try to connect it from javascript the server immediately breaks and I get this error.
PHP Fatal Error ‘yii\base\ErrorException’ with message 'During inheritance of ArrayAccess: Uncaught yii\base\ErrorException: Return type of Guzzle\Common\Collection::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Applications/MAMP/htdocs/namenja/vendor/guzzle/common/Guzzle/Common/Collection.php:294
Here is my console controller code for the server.
<?php
namespace console\controllers;
use consik\yii2websocket\WebSocketServer;
use yii\console\Controller;
class ServerController extends Controller
{
public function actionStart()
{
$server = new WebSocketServer();
$server->port = 80; //This port must be busy by WebServer and we handle an error
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN_ERROR, function($e) use($server) {
echo "Error opening port " . $server->port . "\n";
$server->port += 1; //Try next port to open
$server->start();
});
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN, function($e) use($server) {
echo "Server started at port " . $server->port;
});
$server->start();
}
}
and here is the js code
var conn = new WebSocket('ws://localhost:81');
conn.onmessage = function(e) {
console.log('Response:' + e.data);
};
conn.onopen = function(e) {
console.log("Connection established!");
console.log('Hey!');
conn.send('Hey!');
};```
The full trace of the error can be seen here.
[https://prnt.sc/jasG87i7WgF5](https://prnt.sc/jasG87i7WgF5)