国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

phpsocket.io 中認(rèn)證失敗的客戶端連接如何服務(wù)端主動斷開

609176445

我修改啟動腳本如下,@walkor ,幫忙看看要怎么斷開非法的請求連接呢?

<?php
use Workerman\Worker;
use Workerman\WebServer;
use Workerman\Autoloader;
use PHPSocketIO\SocketIO;

// composer autoload
include __DIR__ . '/../../vendor/autoload.php';
include __DIR__ . '/../../src/autoload.php';

$io = new SocketIO(2020);
define('USER', "randy");
define('PWD', '123');
$io->on('connection', function ($socket) use ($io) {
    $socket->addedUser = false;
    $socket->auth = false;
    \Workerman\Lib\Timer::add(1, function () use ($socket, $io) {
        if (!$socket->auth && $socket) {
            unset($socket);
        }
    }, [], false);

    $socket->on("doauth", function ($account) use ($socket) {
        //do auth
        $account = @json_decode($account, true);
        if (!is_array($account)) {
            $socket->emit("auth fail", );
            unset($socket);
            return;
        } elseif ($account != USER || $account != PWD) {
            $socket->emit("auth fail", );
            unset($socket);
            return;
        }

        $socket->auth = true;
        // when the client emits 'new message', this listens and executes
        $socket->on('new message', function ($data) use ($socket) {
            // we tell the client to execute 'new message'
            $socket->broadcast->emit('new message', array(
                'username' => $socket->username,
                'message'  => $data
            ));
        });

        // when the client emits 'add user', this listens and executes
        $socket->on('add user', function ($username) use ($socket) {
            global $usernames, $numUsers;
            // we store the username in the socket session for this client
            $socket->username = $username;
            // add the client's username to the global list
            $usernames = $username;
            ++$numUsers;
            $socket->addedUser = true;
            $socket->emit('login', array(
                'numUsers' => $numUsers
            ));
            // echo globally (all clients) that a person has connected
            $socket->broadcast->emit('user joined', array(
                'username' => $socket->username,
                'numUsers' => $numUsers
            ));
        });

        // when the client emits 'typing', we broadcast it to others
        $socket->on('typing', function () use ($socket) {
            $socket->broadcast->emit('typing', array(
                'username' => $socket->username
            ));
        });

        // when the client emits 'stop typing', we broadcast it to others
        $socket->on('stop typing', function () use ($socket) {
            $socket->broadcast->emit('stop typing', array(
                'username' => $socket->username
            ));
        });

        // when the user disconnects.. perform this
        $socket->on('disconnect', function () use ($socket) {
            global $usernames, $numUsers;
            // remove the username from global usernames list
            if ($socket->addedUser) {
                unset($usernames);
                --$numUsers;

                // echo globally that this client has left
                $socket->broadcast->emit('user left', array(
                    'username' => $socket->username,
                    'numUsers' => $numUsers
                ));
            }
        });
    });

});

$web = new WebServer('http://0.0.0.0:2022');
$web->addRoot('localhost', __DIR__ . '/public');

Worker::runAll();
6906 3 0
3個回答

walkor 打賞

socket.io好像沒有關(guān)閉鏈接的方法。

  • 暫無評論
609176445

有個 socket.io-auth 好像是服務(wù)端直接disconnect客戶端連接。不然直接部署到線上,任何非法連接不授權(quán)都可以很容易被攻擊掛了,我用PHP unset socket好像連接都還在。不知道有沒辦法實現(xiàn)

  • 暫無評論
liulingyin

@609176445 我也有這樣的問題,unset socket后連接都還在,現(xiàn)在解決了嗎。

  • 暫無評論
年代過于久遠(yuǎn),無法發(fā)表回答
??