首页 php教程 PHP源码 PHP+socket推送消息到DDpush服务器

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

May 23, 2016 pm 01:07 PM

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(&#39;Wrong strHost or Wrong intPort&#39;);
        } elseif (($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
            throw new Exception(&#39;Error at socket_create(): &#39; . socket_strerror(socket_last_error()));
        } elseif (socket_connect($this->socket, $strHost, $intPort) === false) {
            throw new Exception(&#39;Error at socket_connect(): &#39; . 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(&#39;CCCH32n&#39;, $intVersion, $intAppid, 16, $strUUID, 0);
            socket_write($this->socket, $strBin, strlen($strBin)) && $blnRe = (bindec(socket_read($this->socket, 1)) == 0);
        } else {
            throw new Exception(&#39;Error at push0x10()&#39;);
        }
        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(&#39;CCCH32nH16&#39;, $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(&#39;Error at push0x11()&#39;);
        }
        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, &#39;utf8&#39;, &#39;gbk&#39;);
        $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(&#39;CCCH32nA&#39; . $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(&#39;Error at push0x20()&#39;);
        }
        return $blnRe;
    }
    
    /**
     * 断开连接
     */
    public function __destruct() {
        if ($this->socket) {
            socket_close($this->socket);
            $this->socket = null;
        }
    }
    
}

/**
 * 使用Demo
 */
try {
    $obj = new DDpusher(&#39;172.16.14.7&#39;);
    $obj->push0x10(&#39;user01&#39;) && print(&#39;通知已发送<br />&#39;);
    $obj->push0x11(&#39;user01&#39;, &#39;0000000000000001&#39;) && print(&#39;分类已发送<br />&#39;);
    $obj->push0x20(&#39;user01&#39;, &#39;我爱你DDpush&#39;) && print(&#39;字符串消息已发送<br />&#39;);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)