public function clientSend(array $data) { if (!empty($this->client) && $this->client->getState() == WebSocketClient::STATE_CONNECTED) { $this->client->send(json_encode(array_merge(['id' => $this->client_incremental++], $data))); return true; } return false; }
public static function addBot($bot, Logger $logger) { if (is_array($bot) && !isset($bot['bot_token'])) { throw new \Exception('no_bot_token'); } $loop = Factory::create(); $client = new WebSocketClient('ws://0.0.0.0:12345/', $loop, $logger); $client->on("error", function () use($loop, $logger) { $logger->err("Add team incoming webhook : can't connect to websocket server"); $loop->stop(); }); $client->on("connect", function () use($client, $loop, $logger, $bot) { $logger->notice("Add team incoming webhook : " . json_encode($bot)); $loop->addTimer(2, function () use($client, $loop, $bot) { $message = ['type' => 'add_bot', 'bot' => $bot]; $client->send(json_encode($message)); $loop->stop(); }); }); $client->open(); $loop->run(); }