laravel 之 swoolephp
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Redis; use phpDocumentor\Reflection\Types\Null_; class Swoole extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'swoole:action {action}'; /** * The console command description. * * @var string */ protected $description = 'swoole command'; /** * @var Message */ protected $message; /** * @var User */ protected $user; /** * @var Room */ protected $room; private $server; private $fid=[]; /** * Swoole constructor. * @param Message $message * @param User $user * @param RoomJoin $room */ public function __construct( ) { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $action = $this->argument('action'); switch ($action) { case 'start': $this->start(); break; case 'stop': $this->stop(); break; case 'restart': $this->restart(); break; } } /** * 開啓websocket */ private function start() { $this->server = new \swoole_websocket_server(config("swoole.host"), config("swoole.port"), SWOOLE_BASE, SWOOLE_SOCK_TCP); //SWOOLE_SSL 須要ssl才加 #監聽WebSocket鏈接打開事件 $this->server->on('open', function ($server, $request) { // 驗證連接 $fdinfo = $this->server->getClientInfo($request->fd); if($fdinfo['remote_ip'] != '127.0.0.1' && $fdinfo['server_port'] != '9502') return; $this->server->push($request->fd, '["pang"]'); // 驗證連接 $fdinfo = $this->server->getClientInfo($request->fd); if($fdinfo['remote_ip'] != '127.0.0.1' && $fdinfo['server_port'] != '9502') return; $this->fid[]=$request->fd; # $request->fd fd }); #監聽WebSocket消息事件 $this->server->on('message', function ($server, $frame) { #$frame->data 消息內容 $msg = 'from' . $frame->fd . ":{$frame->data}\n"; \Log::info("message :".$msg); // if($frame->data != 'pings') return; // 驗證連接 $fdinfo = $this->server->getClientInfo($frame->fd); if($fdinfo['remote_ip'] != '127.0.0.1' && $fdinfo['server_port'] != '9502') return; foreach ($this->fid as $fd) { $server->push($fd, $msg); } \Log::info("fds :".json_encode($this->fid)); }); //監聽WebSocket鏈接關閉事件 $this->server->on('close', function($ws, $fd) { // 驗證連接 $fdinfo = $this->server->getClientInfo($fd); if($fdinfo['remote_ip'] != '127.0.0.1' && $fdinfo['server_port'] != '9502') return; \Log::info("client info:".json_encode($fdinfo)); \Log::info("list fd:".json_encode($this->fid)); $fd_key = array_search($fd, $this->fid ? $this->fid : []); $key_zero = isset($this->fid[0]) && $this->fid[0] == $fd ? TRUE : FALSE; # key=0 if ($fd_key || $key_zero) { unset($this->fid[$fd_key]); } \Log::info("client-{$fd} is closed\n"); }); #onRequest回調 http://127.0.0.1:9502/?sendto=1,20,3&message=%E4%BD%A0%E5%A5%BD $this->server->on('request', function ($req, $respone) { // 驗證連接 $fdinfo = $this->server->getClientInfo($respone->fd); if($fdinfo['remote_ip'] != '127.0.0.1' && $fdinfo['server_port'] != '9502') $respone->end("404 not found"); # get 兩個參數, userid "," 發送消息 $list=[]; if (isset($req->get['message'])) { $list = $this->fid; if (!empty($list)) { foreach ($list as $fd) { $this->server->push($fd, $req->get['message']); } } } $total= count($this->fid); $sendSum= count($list); $respone->end("Current fid:{$respone->fd}, OnLine:{$total}, Send:{$sendSum}"); }); $this->server->start(); } /** * 中止websocket */ private function stop() { } /** * 重啓 */ private function restart() { } }