server.php 557 B

123456789101112131415161718192021222324
  1. <?php
  2. $ws = new swoole_websocket_server("127.0.0.1", 9501, SWOOLE_BASE);
  3. $ws->set(array(
  4. 'log_file' => '/dev/null'
  5. ));
  6. $ws->on("WorkerStart", function (\swoole_server $serv) {
  7. });
  8. $ws->on('open', function ($serv, swoole_http_request $request) {
  9. //$ip = co::gethostbyname('www.baidu.com');
  10. if (1) {
  11. $serv->push($request->fd, "start\n");
  12. }
  13. });
  14. $ws->on('message', function ($serv, $frame) {
  15. var_dump($frame);
  16. co::sleep(0.1);
  17. $data = $frame->data;
  18. $serv->push($frame->fd, "hello client {$data}\n");
  19. });
  20. $ws->start();