start_gateway.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. use \Workerman\Worker;
  15. use \GatewayWorker\Gateway;
  16. use \Workerman\Autoloader;
  17. require_once __DIR__ . '/../../vendor/autoload.php';
  18. // gateway 进程
  19. $gateway = new Gateway("tcp://0.0.0.0:2346");
  20. // 设置名称,方便status时查看
  21. $gateway->name = 'ChatGateway';
  22. // 设置进程数,gateway进程数建议与cpu核数相同
  23. $gateway->count = 4;
  24. // 分布式部署时请设置成内网ip(非127.0.0.1)
  25. $gateway->lanIp = '127.0.0.1';
  26. // 内部通讯起始端口。假如$gateway->count=4,起始端口为2300
  27. // 则一般会使用2300 2301 2302 2303 4个端口作为内部通讯端口
  28. $gateway->startPort = 2347;
  29. // 心跳间隔
  30. $gateway->pingInterval = 10;
  31. // 心跳数据
  32. $gateway->pingData = '{"type":"ping"}';
  33. // 服务注册地址
  34. $gateway->registerAddress = '127.0.0.1:2345';
  35. /*
  36. // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
  37. $gateway->onConnect = function($connection)
  38. {
  39. $connection->onWebSocketConnect = function($connection , $http_header)
  40. {
  41. // 可以在这里判断连接来源是否合法,不合法就关掉连接
  42. // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
  43. if($_SERVER['HTTP_ORIGIN'] != 'http://chat.workerman.net')
  44. {
  45. $connection->close();
  46. }
  47. // onWebSocketConnect 里面$_GET $_SERVER是可用的
  48. // var_dump($_GET, $_SERVER);
  49. };
  50. };
  51. */
  52. // 如果不是在根目录启动,则运行runAll方法
  53. if(!defined('GLOBAL_START'))
  54. {
  55. Worker::runAll();
  56. }