您好,欢迎访问一九零五行业门户网

PHP+socket推送消息到DDpush服务器

php代码
<?php /** * 用socket向ddpush服务器发送消息 * * @author wang wenbing<binny_w@qq.com> */ class ddpusher { /* socket resource */ private $socket = null; /** * 构造函数 * @param string $strhost * @param int $intport * @throws exception */ public function __construct($strhost, $intport = 9999) { $strhost = strval($strhost); $intport = intval($intport); if (empty($strhost) || !$intport) { throw new exception('wrong strhost or wrong intport'); } elseif (($this->socket = socket_create(af_inet, sock_stream, sol_tcp)) === false) { throw new exception('error at socket_create(): ' . socket_strerror(socket_last_error())); } elseif (socket_connect($this->socket, $strhost, $intport) === false) { throw new exception('error at socket_connect(): ' . socket_strerror(socket_last_error())); } } /** * 生成32位的uuid,可以重写此函数 * @param string $struser * @return string(32) */ private function getuuid($struser) { $struser = trim($struser); return strlen($struser) ? md5($struser) : false; } /** * 检查version和appid参数 * @param int $intversion * @param int $intappid * @return boolen */ private function checkversionandappid($intversion, $intappid) { return ($intversion > 0 && $intversion < 256 && $intappid > 0 && $intappid < 256); } /** * 发送通知 * @param string $struser * @return boolean $blnre */ public function push0x10($struser, $intversion = 1, $intappid = 1) { $blnre = false; $intversion = intval($intversion); $intappid = intval($intappid); if ($this->checkversionandappid($intversion, $intappid) && ($struuid = $this->getuuid($struser)) !== false && $this->socket) { $strbin = pack('ccch32n', $intversion, $intappid, 16, $struuid, 0); socket_write($this->socket, $strbin, strlen($strbin)) && $blnre = (bindec(socket_read($this->socket, 1)) == 0); } else { throw new exception('error at push0x10()'); } return $blnre; } /** * 发送分类信息 * @param string $struser * @param string $strhex 16位长的16进制字符 * @param int $intversion * @param int $intappid * @return boolen $blnre */ public function push0x11($struser, $strhex, $intversion = 1, $intappid = 1) { $blnre = false; $intversion = intval($intversion); $intappid = intval($intappid); $strhex = trim($strhex); if ($this->checkversionandappid($intversion, $intappid) && ($struuid = $this->getuuid($struser)) !== false && $this->socket && strlen($strhex) == 16) { $strbin = pack('ccch32nh16', $intversion, $intappid, 17, $struuid, 8, $strhex); socket_write($this->socket, $strbin, strlen($strbin)) && $blnre = (bindec(socket_read($this->socket, 1)) == 0); } else { throw new exception('error at push0x11()'); } return $blnre; } /** * 发送500字节以内的字符消息 * @param string $struser * @param string $strmsg 必须是utf8编码的字符 * @param int $intversion * @param int $intappid * @return boolen $blnre * @throws exception */ public function push0x20($struser, $strmsg, $intversion = 1, $intappid = 1) { $blnre = false; $intversion = intval($intversion); $intappid = intval($intappid); // $strmsg = mb_convert_encoding($strmsg, 'utf8', 'gbk'); $strmsg = trim($strmsg); $intlen = strlen($strmsg); $blntemp = ($intlen > 0 && $intlen <= 500); if ($this->checkversionandappid($intversion, $intappid) && ($struuid = $this->getuuid($struser)) !== false && $this->socket && $blntemp) { $strbin = pack('ccch32na' . $intlen, $intversion, $intappid, 32, $struuid, $intlen, $strmsg); socket_write($this->socket, $strbin, strlen($strbin)) && $blnre = (bindec(socket_read($this->socket, 1)) == 0); } else { throw new exception('error at push0x20()'); } return $blnre; } /** * 断开连接 */ public function __destruct() { if ($this->socket) { socket_close($this->socket); $this->socket = null; } } } /** * 使用demo */ try { $obj = new ddpusher('172.16.14.7'); $obj->push0x10('user01') && print('通知已发送<br />'); $obj->push0x11('user01', '0000000000000001') && print('分类已发送<br />'); $obj->push0x20('user01', '我爱你ddpush') && print('字符串消息已发送<br />'); } catch (exception $ex) { echo $ex->getmessage(); }
其它类似信息

推荐信息