Answer the question
In order to leave comments, you need to log in
WebSocket Yii consik not getting a response from the server from what could it be?
Here is the server controller
<?php
namespace app\commands;
use consik\yii2websocket\WebSocketServer;
use yii\console\Controller;
class ServerController extends Controller
{
public function actionStart()
{
$server = new WebSocketServer();
$server->port = '8090';
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN_ERROR, function($e) use($server) {
echo "Error opening port " . $server->port . "\n";
// $server->port += 1;
$server->start();
});
$server->on(WebSocketServer::EVENT_WEBSOCKET_OPEN, function($e) use($server) {
echo "Server started at port " . $server->port;
});
$server->start();
}
}
<?php
namespace app\daemons;
use consik\yii2websocket\WebSocketServer;
use Ratchet\ConnectionInterface;
class CommandsServer extends WebSocketServer
{
protected function getCommand(ConnectionInterface $from, $msg)
{
return $msg;
}
function commandPing(ConnectionInterface $client, $msg)
{
$client->send('Pong');
}
}
var socket = new WebSocket('ws://127.0.0.1:8090');
socket.onopen = () => socket.send('ping');
socket.onclose = function(event) {
if (event.wasClean) {
alert('Соединение закрыто чисто');
} else {
alert('Обрыв соединения');
}
alert('Код: ' + event.code + ' причина: ' + event.reason);
};
socket.onmessage = function (event) {
console.log(event.data);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question