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) { } } }
<?php require 'ElephantIOClient.class.php'; $elephant = new ElephantIOClient('http://127.0.0.1:8080'); $elephant->init(); $elephant->send(ElephantIOClient::TYPE_MESSAGE, null, null, 'Hello World!'); $elephant->keepAlive();
/** * 初始化 * * @param callable $initCallback 初始化结果回调 * @param callable $recCallback 重新连接回调 */ public function init($initCallback = null, $recCallback = null) { if (is_callable($initCallback)) { $this->_initCallback = $initCallback; } if (is_callable($recCallback)) { $this->_recCallback = $recCallback; } $this->on("socketconnectack", array($this, "_initCallbackMethod")); $this->on("connack", array($this, "_connectCallbackMethod")); $this->on("disconnect", array($this, "_reconnectCallbackMethod")); $this->on("error", array($this, "_disconnectCallbackMethod")); $this->on("reconnect", array($this, "_disconnectCallbackMethod")); $this->on("reconnect_failed", array($this, "_disconnectCallbackMethod")); $this->on("suback", array($this, "_subscribeCallbackMethod")); $this->on("message", array($this, "_messageCallbackMethod")); $this->on("puback", array($this, "_publishCallbackMethod")); $this->on("unsuback", array($this, "_unsubscribeCallbackMethod")); $this->_client->init(); }
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(); } } }