function iotoserver($messageArr) { if (defined('NODEJS_HOST') && trim(NODEJS_HOST)) { try { $elephant = new ElephantIOClient(NODEJS_HOST, 'socket.io', 1, false, false, true); $elephant->setHandshakeTimeout(1000); $elephant->init(); $elephant->send(ElephantIOClient::TYPE_EVENT, null, null, json_encode(array('name' => 'iotoserver', 'args' => $messageArr))); $elephant->close(); } catch (Exception $e) { echo $e->getMessage(); } } }
function iotoserver($messageArr) { if (defined('NODEJS_HOST') && trim(NODEJS_HOST)) { App::import('Vendor', 'ElephantIO', array('file' => 'ElephantIO' . DS . 'Client.php')); try { $elephant = new ElephantIOClient(NODEJS_HOST, 'socket.io', 1, false, false, true); $elephant->setHandshakeTimeout(1000); $elephant->init(); $elephant->send(ElephantIOClient::TYPE_EVENT, null, null, json_encode(array('name' => 'iotoserver', 'args' => $messageArr))); $elephant->close(); } catch (Exception $e) { } } }
/** * 等待通讯 */ public function wait() { while (true) { if ($this->_client) { $this->_client->keepAlive(); } if ($this->_autoReconnect === false) { break; } else { if ($this->_client) { $this->_client->close(); $this->_client = null; } $this->_messageCallbacks = array(); try { $this->_client = new ElephantIOClient("http://" . $this->_server . ":" . $this->_port, "socket.io", 1, false, true, $this->_debug); $this->init(); if ($this->_useSessionId) { $this->connect_v2(); } else { $this->connect(); } } catch (Exception $e) { if ($this->_client) { $this->_client->close(); $this->_client = null; } sleep(5); } } } }