学生教材网 >程序设计 > 代码分享 > PHP > 浏览文章

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

来源:网络编辑:佚名时间:2015-10-17人气:

镣铐小说,最后一名博学者,断风贤直播

学生教材网的小编为您介绍PHP+socket推送消息到DDpush服务器的具体内容:

PHP+Socket --tcp--> DDpush Server --udp--> Client(Android)
我分享的代码只是实现PHP+Socket
应该比这篇Blog强一些吧:http://blog.csdn.net/brok1n/article/details/45272573 标签: DDPush任意门推送

代码片段(1) [全屏查看所有代码]

1. [文件] DDpusher.class.php ~ 5KB     下载(0)     跳至 [1] [全屏预览]

<?php
/**
 * 用Socket向DDpush服务器发送消息
 * 相关文档,请参考http://www.ddpush.net
 * @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;
    }
    
    /**
     * 发送分类信息,此处考虑到部分机型最大只能支持32位,所以将8字节(64位)分为4份,每份16位,每位最大数65535
     * @param string $strUser
     * @param int $int16
     * @param int $int32
     * @param int $int48
     * @param int $int64
     * @param int $intVersion
     * @param int $intAppid
     * @return boolen $blnRe
     */
    public function push0x11($strUser, $int16 = 0, $int32 = 0, $int48 = 0, $int64 = 0, $intVersion = 1, $intAppid = 1) {
        $blnRe = false;
        $intVersion = intval($intVersion);
        $intAppid = intval($intAppid);
        $int16 = intval($int16);
        $int32 = intval($int32);
        $int48 = intval($int48);
        $int64 = intval($int64);
        $blnTemp = ($int16 >= 0 && $int16 <= 65535); // 2^16 = 65536
        $blnTemp && $blnTemp = ($int32 >= 0 && $int32 <= 65535);
        $blnTemp && $blnTemp = ($int48 >= 0 && $int48 <= 65535);
        $blnTemp && $blnTemp = ($int64 >= 0 && $int64 <= 65535);
        if ($this->checkVersionAndAppid($intVersion, $intAppid) && ($strUUID = $this->getUUID($strUser)) !== false && $this->socket && $blnTemp) {
            $strBin = pack('CCCH32nnnnn', $intVersion, $intAppid, 17, $strUUID, 8, $int64, $int48, $int32, $int16);
            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', 0, 0, 1) && print('分类已发送<br />');
    $obj->push0x20('user01', '我爱你DDpush') && print('字符串消息已发送<br />');
} catch (Exception $ex) {
    echo $ex->getMessage();
}
*/

以上内容是由学生教材网的小编为您介绍的.

php 抓取 百度热词 搜索的 http://top.baidu.com…

前面开发PHP的过程中、有一个网站要做一个导航的、需要用到百度热词、百度搜索榜的TOP50。

热门推荐