A
A
Artem Prokhorov2021-09-07 15:54:06
Yii
Artem Prokhorov, 2021-09-07 15:54:06

Does Yii2 have normal websockets?

Consik, the documentation of which does not even give an example of how to close this very socket (ctrl + c will do everything, only in this case the trigger for closing the connection will not work), and if you dig into the code yourself, it turns out that any an attempt to access the server outside the command actionStartturns out to be simply in the xxxxxx ritternet console, because the properties are not public, and when they are changed to public, for some reason the result remains the same.

Also, there are no methods at all for the possibility of how to check the client at the moment when trying to connect, but before the connection itself. Well, that is, roughly speaking, this is the simplest implementation for a chat, and any attempt to get a result not described in the documentation examples simply does not work for you.
The rest are in Chinese and there is kind of like 1 normal for primers, etc., etc., but it just doesn’t install, since it was apparently developed under Yii1.

What to use then?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2021-09-07
@kotcich

Why exactly under yii are you looking for. Take it wider)
https://github.com/walkor/Workerman
I use it myself on Yii2 and everything is fine.
You can look at my GK :D
But it's better to rewrite it, of course, with adequate code, because I wrote it to write it, it works and okay

chat on websockets

<?php

use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';

$unique_users = [];
$context = array(
    'ssl' => array(
        'local_cert'  => __DIR__ . '/privatessl/cert.pem',
        'local_pk'    => __DIR__ . '/privatessl/key.key',
        'verify_peer' => false,
    )
);

// Create a Websocket server with ssl context.
$ws_worker = new Worker('websocket://website.ru:6374', $context);

$ws_worker->transport = 'ssl';

$ws_worker->onMessage = function ($connection, $data) use ($ws_worker, &$unique_users) {
    $response = json_decode($data);
    ## Команды ##
    if (isset($response->command)) {

        ## Получение пользователей онлайн ##
        if ($response->command == 'getOnline') {

            $connection->send(getOnline($unique_users));
        }
        ## Отправка нового сообщения ##
        if ($response->command == 'sendMessage' && isset($response->message) && !empty($response->message)) {
            sendMessage(
                $response->message,
                $response->login,
                $response->avatar,
                $ws_worker,
                $connection
        );
        }
    }
};
$ws_worker->onConnect = function($connection) use (&$unique_users)
{
    $ip = $connection->getRemoteIp();
    if (!in_array($ip, $unique_users))
    {
        $unique_users[] = $ip;
    }
};
$ws_worker->onClose = function($connection) use (&$unique_users)
{
    $ip = $connection->getRemoteIp();
    $index = array_search($ip, $unique_users);
    unset($unique_users[$index]);
};
function getOnline($unique_users)
{
    $online = [
        'action' => 'online',
        'body' => count($unique_users),
    ];
    return json_encode($online);
}
function sendMessage($message,$login, $avatar, $worker, $connection)
{
    $body = [
        'action' => 'newMessage',
        'body' => $message,
        'login' => $login,
        'avatar' => $avatar,
        'time' => date('d.m.Y в H:i', (time() + 10800))
    ];
    foreach ($worker->connections as $client) {
        $client->send(json_encode($body));
    }
    addMessageDB($message, $login, $avatar);
}
function prepareText($text)
{
    return htmlspecialchars($text);
}
function addMessageDB($message, $login, $avatar){
    $db_config = require(__DIR__ . '/config/db.php');
    $db = new PDO($db_config['dsn'] . ';charset=' . $db_config['charset'], $db_config['username'], $db_config['password']);
    $sth = $db->prepare('INSERT INTO chat_message (message, created_at, login, avatar) VALUES (:mess, :create_time, :login, :avatar)');
    $sth->execute([
        ':mess' => prepareText($message),
        ':create_time' => time(),
        ':login' => prepareText($login),
        ':avatar' => prepareText($avatar),
    ]);
    return $sth->fetchAll(PDO::FETCH_ASSOC);
}
Worker::runAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question