chatser.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. $server = new swoole_websocket_server('0.0.0.0', 9501);
  3. $server->on("open", "openServer");
  4. $server->on("message", "messageServer");
  5. $server->on("close", "closeServer");
  6. $server->on("workerStart", 'isOnWorkerStart');
  7. $user_id = 0;
  8. function isOnWorkerStart($server, $worker_id)
  9. {
  10. include "autoload.php";
  11. log_message::info(" isOnWorkerStart ");
  12. }
  13. /**
  14. * open server
  15. * @param $server
  16. * @param $request
  17. */
  18. function openServer($server, $request)
  19. {
  20. log_message::info(" openServer ", $request);
  21. echo "server: handshake success with fd{$request->fd}";
  22. }
  23. function messageServer($server, $frame)
  24. {
  25. //require "autoload.php";
  26. log_message::info("@@@@@@@@@@@@@@@client messageServer Info", " server fd", $frame->fd, " server data", $frame->data);
  27. $chat = new ChatModel($server, $frame->fd, $frame->data);
  28. log_message::info("@@@@@@@@@@@@@@@client messageServer in class chatmodel");
  29. echo $chat;
  30. // 解包
  31. // 处理之后返回发包开始
  32. // MODE 里进行处理
  33. // 对应用户数据
  34. /*foreach ($server->connections as $key => $fd) {
  35. $user_message = $frame->data;
  36. log_message::info(" messageServer ", $user_message);
  37. $server->push($fd, "fd1212121" . $user_message);
  38. }*/
  39. }
  40. function closeServer($ser, $fd)
  41. {
  42. $data = ['pact' => 'quit'];
  43. $chat = new ChatModel($ser, $fd, $data);
  44. log_message::info(" client ", $fd, $data);
  45. echo "client {$fd} closed\n";
  46. }
  47. $server->start();