A
A
Anon33632020-08-20 21:22:30
Yii
Anon3363, 2020-08-20 21:22:30

WebSocket Yii consik not getting a response from the server from what could it be?

5f3ebe54d528a916884010.png5f3ebe5bd02c3563232948.png5f3ebe6640bf4917659839.png

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();
    }
}


Here is the deamon that should reply to the message
<?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');
    }

}


JavaScript
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 question

Ask a Question

731 491 924 answers to any question