-
-
Save snailsmall612/15534af7cba04d19e06d0e78f67558bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use swoole_websocket_server; | |
class Swoole extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'swoole {action}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$action = $this->argument('action'); | |
switch ($action) { | |
case 'close': | |
break; | |
case 'start': | |
$this->start(); | |
break; | |
default: | |
break; | |
} | |
} | |
private function start() | |
{ | |
$this->ws = new swoole_websocket_server("0.0.0.0", 9502); | |
$this->ws->on('open', function ($ws, $request) { | |
$this->info("WebSocket connect open! fd: " . $request->fd); | |
$this->ws->push($request->fd, "welcome!"); | |
}); | |
$this->ws->on('message', function ($ws, $frame) { | |
$this->info("client message: " . $frame->data); | |
$this->ws->push($frame->fd, "this is server"); | |
}); | |
$this->ws->on('close', function ($ws, $fd) { | |
$this->info("client is close\n"); | |
}); | |
$this->ws->start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment