PHP实现微信扫码登陆(四)--使用workerman搭建websocket服务器

因为使用laravel, 所以使用artisan编写服务,
也可以使用其他文件编写, 直接用php 命令执行, 参照博客
PHP使用workerman实现后端消息推送前端, 完成微信登录
建议先读一下上边这边博客, 有助于读者理解整个推送过程

  • 代码如下
    app/Console/Commands/WebsocketServer
argument('action');
        $argv[1] = $argv[2];
        $argv[2] = isset($argv[3]) ? "-{$argv[3]}" : '';
        switch ($arg) {
            case 'start':
                $this->start();
                break;
            case 'stop':
                break;
            case 'restart':
                break;
            case 'reload':
                break;
            case 'status':
                break;
            case 'connections':
                break;
        }

    }

    private function start(){
        // 使用websocket协议, 监听端口, 等待客户端发起websocket连接
        $this->worker = new Worker('websocket://0.0.0.0:18888');
        // 保存客户端连接
        $this->worker->uidConnections = [];
        $this->worker->count = 1;
        $this->worker->onWorkerStart = function ($worker) {
            // 创建内部连接, 用于接收扫码后, php服务器推送的消息
            $inner_text_worker = new Worker('tcp://0.0.0.0:18889');
            $inner_text_worker->onMessage = function ($connection, $buffer) {
                Log::alert('workerman inner accept message : ' . $buffer);
                $data = json_decode($buffer, true);
                $uid = $data['scene'];
                $token = $data['token'];
                // 向浏览器推送用户标识
                $res = $this->sendMessageByUid($uid, $token);
                Log::alert('workerman reply message to uid : '. $uid .'..$openid..'. $token);
                $mess = $res ? 'ok' : 'fail';
                Log::alert('workerman reply message : ' . $mess);
                // 通知php客户端推送结果
                $connection->send($mess);
            };
            $inner_text_worker->listen();
        };

        $this->worker->onMessage = function ($connection, $data) {
            Log::alert('workerman accept browser data : ' . $data);
            if (!isset($connection->uid)) {
                $connection->uid = $data;
                $this->worker->uidConnections[$connection->uid] = $connection;
                return;
            }
        };

        $this->worker->onClose = function ($connection) {
            if (isset($connection->uid)) {
                Log::alert('workerman close browser : ' . $connection->uid);
                unset($this->worker->uidConnections[$connection->uid]);
            }
        };

        Worker::runAll();
    }

    /**
     * 消息广播
     * @param $message
     */
    private function broadCast($message)
    {
        global $worker;
        foreach ($worker->uidConnections as $connection) {
            $connection->send($message);
        }
    }

    private function sendMessageByUid($uid, $message)
    {
        Log::alert(json_encode($this->worker->uidConnections));
        if (isset($this->worker->uidConnections[$uid])) {
            $connection = $this->worker->uidConnections[$uid];
            $connection->send($message);
            return true;
        }
        return false;
    }
}

在app/Console/Kernel.php中注册

protected $commands = [
        //
        WebsocketServer::class,
    ];
  • 进入到项目根目录, 启动, 等待连接
    php artisan websocket start

你可能感兴趣的:(PHP实现微信扫码登陆(四)--使用workerman搭建websocket服务器)