Skip to content

Instantly share code, notes, and snippets.

@snailsmall612
Created May 22, 2022 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snailsmall612/15534af7cba04d19e06d0e78f67558bf to your computer and use it in GitHub Desktop.
Save snailsmall612/15534af7cba04d19e06d0e78f67558bf to your computer and use it in GitHub Desktop.
<?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