'send', 'd' => 'daemon', 'g' => 'gracefully', ]; } public function actionIndex() { if ('start' == $this->send) { try { $this->start($this->daemon); } catch (\Exception $e) { $this->stderr($e->getMessage() . "\n", Console::FG_RED); } } else if ('stop' == $this->send) { $this->stop(); } else if ('restart' == $this->send) { $this->restart(); } else if ('reload' == $this->send) { $this->reload(); } else if ('status' == $this->send) { $this->status(); } else if ('connections' == $this->send) { $this->connections(); } } public function initWorker() { $ip = isset($this->config['ip']) ? $this->config['ip'] : $this->ip; $port = isset($this->config['port']) ? $this->config['port'] : $this->port; $wsWorker = new Worker("websocket://{$ip}:{$port}"); // 4 processes $wsWorker->count = 4; // Emitted when new connection come $wsWorker->onConnect = function ($connection) { echo "New connection\n"; }; // Emitted when data received $wsWorker->onMessage = function ($connection, $data) { // ssl需要访问443端口 $con = new \Workerman\Connection\AsyncTcpConnection("ws://stream.binance.com:9443/ws/!ticker@arr"); // 设置以ssl加密方式访问,使之成为wss $con->transport = 'ssl'; // $con->onConnect = function($con) { // $data = json_encode([ // 'sub' => 'market.btcusdt.kline.1min', // 'id' => 'depth' . time() // ]); // $con->send($data); // }; $con->onMessage = function($con, $data) use($connection) { $connection->send(date("Y-m-d H:i:s") . ' : ' . $data); }; $con->connect(); }; // Emitted when connection closed $wsWorker->onClose = function ($connection) { echo "Connection closed\n"; }; } /** * workman websocket start */ public function start() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'start'; if ($this->daemon) { $argv[2] = '-d'; } // Run worker Worker::runAll(); } /** * workman websocket restart */ public function restart() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'restart'; if ($this->daemon) { $argv[2] = '-d'; } if ($this->gracefully) { $argv[2] = '-g'; } // Run worker Worker::runAll(); } /** * workman websocket stop */ public function stop() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'stop'; if ($this->gracefully) { $argv[2] = '-g'; } // Run worker Worker::runAll(); } /** * workman websocket reload */ public function reload() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'reload'; if ($this->gracefully) { $argv[2] = '-g'; } // Run worker Worker::runAll(); } /** * workman websocket status */ public function status() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'status'; if ($this->daemon) { $argv[2] = '-d'; } // Run worker Worker::runAll(); } /** * workman websocket connections */ public function connections() { $this->initWorker(); // 重置参数以匹配Worker global $argv; $argv[0] = $argv[1]; $argv[1] = 'connections'; // Run worker Worker::runAll(); } }