当前位置:   article > 正文

Laravel+swoole 实现websocket长链接

Laravel+swoole 实现websocket长链接

需要使用 swoole 扩展

我使用的是 swoole 5.x

start 方法启动服务 和 定时器

调整 listenQueue 定时器可以降低消息通讯延迟

定时器会自动推送队列里面的消息

testMessage 方法测试给指定用户推送消息

使用 laravel console 启动

<?php

namespace App\Console\Commands;

use App\Services\SocketService;
use Illuminate\Console\Command;

class WsServer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:wsServer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {

        $SocketService = new SocketService();

        $SocketService->start();

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

socket 服务实现代码

<?php

namespace App\Services;

use Swoole\WebSocket\Server;
use Swoole\Timer;
use Illuminate\Support\Facades\Redis;
use RedisException;
use Swoole\Http\Request;

class SocketService
{

    public $port = 9501;

    public $server;

    public $links;

    public $cmds = [];

    public function __construct ()
    {
        $this->links = collect([]);

        $this->server = new Server("0.0.0.0", env('APP_SOCKET_PORT', $this->port ));

        $this->server->on( 'open', function (Server $server, Request $request){
            $this->open( $server, $request );
        } );

        $this->server->on( 'message', function (Server $server, $frame){
            $this->message( $server, $frame );
        } );

        $this->server->on( 'close', function (Server $server, $fd){
            $this->close( $server, $fd );
        } );
    }

    public function start()
    {
        $this->linkManage();

        $this->listenQueue();

        $this->server->start();
    }

    public function print( $message, $level = 'info' )
    {
        if( is_array($message) || is_object($message) ){
            $message = json_encode($message, 320);
        }

        print_r( "[". date("Y-m-d H:i:s") ."] " . $level . ' ' . $message . "\n" );
    }

    public function linkManage()
    {
        Timer::tick( 100, function (){
            //var_dump( "listenQueue while: " . json_encode($this->cmds, 320) );

            $cmd = array_shift( $this->cmds );
            if( $cmd ){
                switch ( $cmd['operate'] ){

                    case 'open':
                        // 活跃
                        $this->links->push( [ "fd" => $cmd['fd'], "user_id" => intval($cmd['user_id']??0), 'updated_at' => date("Y-m-d H:i:s") ] );

                        $this->print( "添加客户端:fd = " . json_encode($cmd, 320)  );

                        break;

                    case 'close':

                        $newLinks = [];
                        foreach ( $this->links as $link ){
                            if( $link['fd'] == $cmd['fd'] ){
                                continue;
                            }
                            $newLinks[] = $link;
                        }

                        $this->links = collect( $newLinks );

                        $this->print( "删除客户端:fd = " . json_encode($cmd, 320) );

                        break;

                    case 'heartbeat':
                        $newLinks = [];
                        foreach ( $this->links as $link ){
                            if( $link['fd'] == $cmd['fd'] ){
                                $link['updated_at'] = date("Y-m-d H:i:s");
                            }
                            $newLinks[] = $link;
                        }
                        $this->links = collect( $newLinks );
                        break;

                }

                // $this->print( "连接数量是:" . $this->links->count() );
                // $this->print( "连接数量是:" . $this->links->toJson() );
            }

            $newLinks = [];
            foreach ( $this->links as $link ){
                if( strtotime( $link['updated_at'] ) < (time() - 60) ){
                    $this->print( "长时间未心跳,删除客户端:fd = " . json_encode($link, 320) );
                    if( $this->server->isEstablished( $link['fd'] ) ){
                        $this->disconnect( $link['fd'], '未进行心跳' );
                    }
                    continue;
                }
                $newLinks[] = $link;
            }
            $this->links = collect( $newLinks );

        } );
    }

    public function listenQueue()
    {

        Timer::tick( 1000, function (){
            // Redis::rpush( "SocketService:listenQueue", serialize(["hahah"]) )
            try{
                $element = Redis::lpop('SocketService:listenQueue');
                if( $element ){

                    $this->print( "listenQueue 有新的信息哦:" . $element );

                    $data = unserialize($element);

                    if( ! empty( $data['user_id']) ){

                        $links = $this->links->where( "user_id", $data['user_id'] )->values()->all();

                        if( empty($links) ){
                            $this->print( "没有在线用户:user_id = " . json_encode($data, 320) );

                            //var_export( $this->links );
                            //var_export( $links );

                        }

                        foreach ( $links as $link ){
                            if( ! $this->server->isEstablished( $link['fd'] ) ){
                                array_push( $this->cmds, [ 'operate' => 'close', 'fd' => $link['fd'] ] );
                                continue;
                            }

                            try{
                                // 生成消息数据
                                $message = $this->makeMessage( $data['data'], $data['type'], $data['message'] );
                                // 开始推送
                                $this->runPush( $link['fd'], $message );

                            }catch (\Throwable $e){

                                $this->print( "数据推送异常:" . json_encode([ $e->getMessage(),$e->getLine(), $e->getFile() ], 320) );

                            }

                        }
                    }

                }
            }catch (RedisException $e){
                Redis::connect();
            }
        });

    }

    public function open( Server $server, Request $request )
    {
        $params = $request->get;

        if( empty( $params['user_id'] ) ){
            $this->disconnect( $request->fd, '缺少用户信息' );
            return true;
        }

        array_push( $this->cmds, [ 'operate' => 'open', 'fd' => $request->fd, 'user_id' => $params['user_id'] ] );

        // 生成消息数据
        $message = $this->makeMessage( [ 'fd' => $request->fd ], "connectionSuccessful", "连接成功" );
        // 开始推送
        $this->runPush( $request->fd, $message );

        $this->print( "server: handshake success with fd{$request->fd} " );

    }

    public function message( Server $server, $frame )
    {
        //
        $data = json_decode( $frame->data, true );

        if( is_array( $data ) ){
            if( $data['type'] == "ping" ){
                array_push( $this->cmds, [ 'operate' => 'heartbeat', 'fd' => $frame->fd ] );
                $this->server->push( $frame->fd, json_encode( [ "type" => "pong" ] , 320 ) );
            }else{

                $this->print( "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish} " );

            }
        }

    }

    public function close(Server $server, $fd)
    {
        array_push( $this->cmds, [ 'operate' => 'close', 'fd' => $fd ] );

        $this->print( "client {$fd} closed " );

    }

    public function push( $fd, string $data )
    {
        $this->server->push($fd, $data);
    }

    public function disconnect(int $fd, string $reason = '', int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL)
    {
        $this->server->disconnect($fd, $code, $reason);
    }

    public function makeMessage( array $data, $type = "", $message = "" )
    {
        return [ 'type' => $type, "message" => $message, "data" => $data ];
    }

    public function runPush( $fd, $message )
    {
        $this->print( "推送消息: {$fd} - " .  json_encode(  $message, 320 ) );
        $this->server->push( $fd, json_encode( $message , 320 ) );
    }

    /**
     * App\Services\SocketService::testMessage( 92 )
     * @param $user_id
     * @return void
     */
    public static function testMessage( $user_id )
    {
        Redis::rpush( "SocketService:listenQueue", serialize([
            "user_id" => $user_id,
            "type" =>  "testMessage", "message" => "测试消息", "data" => [
                "hello world!"
            ],
        ]) );
    }



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/844371
推荐阅读
相关标签
  

闽ICP备14008679号