K
K
Kulebiaka2021-10-25 22:21:24
Yii
Kulebiaka, 2021-10-25 22:21:24

Problems with WebSocket when creating a chat on yii2?

I create a chat on yii2 via WebSocket, according to this tutorial , the problem is as follows, when loading the page, an error occurs:

Class 'consik\yii2websocket\WebSocketServer' not found.

I did everything clearly according to the instructions, updated the composer, I thought the matter was in the absence of autoloading files, but after that I registered it in the composer
"autoload": {
        "psr-4": {
            "consik\\yii2websocket\\": ""
        }
    },

Everything remains the same.

617703a69384d294164724.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Danchenko, 2021-10-25
@YahorDanchanka

If you are using a ready-made theme (basic in your case), you don’t need to climb into the composer without knowledge. Nothing needs to be added there.
You need to create a "daemon" with its logic:

<?php
namespace app\daemons;

use consik\yii2websocket\events\WSClientMessageEvent;
use consik\yii2websocket\WebSocketServer;

class EchoServer extends WebSocketServer
{

    public function init()
    {
        parent::init();

        $this->on(self::EVENT_CLIENT_MESSAGE, function (WSClientMessageEvent $e) {
            $e->client->send( $e->message );
        });
    }

}

Create command (in yii2 app advanced console folder):
<?php
namespace app\commands;

use app\daemons\EchoServer;
use yii\console\Controller;

class ServerController extends Controller
{
    public function actionStart($port = null)
    {
        $server = new EchoServer();
        if ($port) {
            $server->port = $port;
        }
        $server->start();
    }
}

And now we can start our web socket server: php yii server/start
Observe only the namespaces (the names for the basic template are described above).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question