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

PHP+Socket --tcp--> DDpush Server --udp--> Client(Android) --tcp--> PHP-WEB/API

PHP-WEB/API、DDPush Server、ClientAPP,三方可以愉快地通讯了!

我分享的代码只是实现PHP+Socket

应该比这篇Blog强一些吧:http://blog.csdn.net/brok1n/article/details/45272573

1.[代码][PHP]代码

/**

* 用Socket向DDpush服务器发送消息

* 相关文档,请参考http://www.ddpush.net

* @author Wang Wenbing

*/

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('通知已发送
');

$obj->push0x11('user01', '0000000000000001') && print('分类已发送
');

$obj->push0x20('user01', '我爱你DDpush') && print('字符串消息已发送
');

} catch (Exception $ex) {

echo $ex->getMessage();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值