start_gateway.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = 2350;
  29. // 心跳间隔
  30. $gateway->pingInterval = 10;
  31. // 心跳数据
  32. //$gateway->pingData = '{"type":"ping"}';
  33. $gateway->pingData = '';
  34. // 服务注册地址
  35. $gateway->registerAddress = '127.0.0.1:2345';
  36. /*
  37. // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
  38. $gateway->onConnect = function($connection)
  39. {
  40. $connection->onWebSocketConnect = function($connection , $http_header)
  41. {
  42. // 可以在这里判断连接来源是否合法,不合法就关掉连接
  43. // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
  44. if($_SERVER['HTTP_ORIGIN'] != 'http://chat.workerman.net')
  45. {
  46. $connection->close();
  47. }
  48. // onWebSocketConnect 里面$_GET $_SERVER是可用的
  49. // var_dump($_GET, $_SERVER);
  50. };
  51. };
  52. */
  53. // 如果不是在根目录启动,则运行runAll方法
  54. if(!defined('GLOBAL_START'))
  55. {
  56. Worker::runAll();
  57. }