WebSocketServer.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\Socket\ChatService;
  4. use Illuminate\Console\Command;
  5. use Ratchet\Http\HttpServer;
  6. use Ratchet\Server\IoConnection;
  7. use Ratchet\Server\IoServer;
  8. use Ratchet\WebSocket\WsConnection;
  9. use Ratchet\WebSocket\WsServer;
  10. class WebSocketServer extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'websocket:serve';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Start the WebSocket server by whatsapp';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $server = IoServer::factory(
  41. new HttpServer(
  42. new WsServer(
  43. new ChatService()
  44. )
  45. ),
  46. 8605
  47. );
  48. $server->run();
  49. echo 'socket service start success';
  50. return 0;
  51. }
  52. }