Exemplo n.º 1
0
<?php

require "./global.php";
$socket = stream_socket_server("udp://10.210.136.100:21345", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
    die("{$errstr} ({$errno})");
}
do {
    $pkt = stream_socket_recvfrom($socket, 1, 0, $peer);
    echo "{$peer}\n";
    $listDB = new AlarmListModelDB();
    $messageDB = new AlarmMessageModelDB();
    $userDB = new AlarmUserModelDB();
    $lastId = 0;
    while (1) {
        //$message = pack("h", '01');
        $userId = 1;
        $sql = "SELECT * FROM `" . $listDB->getTableName() . "` WHERE `user_id` = ? AND `id` > ? ORDER BY `id` ASC LIMIT 10";
        $data = $listDB->getData($sql, array($userId, $lastId));
        if (!empty($data)) {
            $messageIds = array();
            foreach ($data as $key => $val) {
                $messageIds[$val['message_id']] = $val['message_id'];
            }
            $sql = "SELECT * FROM `" . $messageDB->getTableName() . "` WHERE `id` IN ?";
            $messageData = $messageDB->getData($sql, array($messageIds));
            $messageFkData = array();
            foreach ($messageData as $v) {
                $messageFkData[$v['id']] = $v;
            }
            $showArr = array();
 public function message()
 {
     require_once dirname(__FILE__) . '/' . 'IGt.Push.php';
     define('APPKEY', 'OlmhhFzNS19UC1oO6aWES3');
     define('APPID', 'EvwdJydwId6ycNVYBRx5N9');
     define('MASTERSECRET', 'CSUV45HwVI7h2LEzRPj8p7');
     define('HOST', 'http://sdk.open.api.igexin.com/apiex.htm');
     function pushNotificationMessageToList($title = "alarm", $text = "是的, 您木有看错..这是测试信息", $clientIds)
     {
         $igt = new IGeTui(HOST, APPKEY, MASTERSECRET);
         //消息类型 :状态栏链接 点击通知打开网页
         $template = new IGtTransmissionTemplate();
         $template->set_appId(APPID);
         //应用appid
         $template->set_appkey(APPKEY);
         //应用appkey
         $template->set_transmissionType(2);
         //透传消息类型
         $template->set_transmissionContent($text);
         //透传内容
         //个推信息体
         $message = new IGtSingleMessage();
         $message->set_isOffline(true);
         //是否离线
         $message->set_offlineExpireTime(3600 * 12);
         //离线时间
         $message->set_data($template);
         //设置推送消息类型
         $contentId = $igt->getContentId($message);
         //接收方1
         $target1 = new IGtTarget();
         $target1->set_appId(APPID);
         foreach ($clientIds as $clientId) {
             $target1->set_clientId($clientId);
             $targetList[] = $target1;
         }
         if (isset($targetList)) {
             $rep = $igt->pushMessageToList($contentId, $targetList);
         }
         return $rep;
     }
     Switcher::close('masterDbPostOnly');
     if (empty($_GET['session']) && empty($_GET['to'])) {
         Message::showError('params error: to or session must not null', 11);
     }
     $listDB = new AlarmListModelDB();
     $messageDB = new AlarmMessageModelDB();
     $userDB = new AlarmUserModelDB();
     if (empty($_GET['to'])) {
         $messageId = $messageDB->getFirst("SELECT `id` FROM `" . $messageDB->getTableName() . "` WHERE `session` = ?", array($_GET['session']));
         $userArr = $listDB->getData("SELECT `user_id` AS `id` FROM `" . $listDB->getTableName() . "` WHERE `message_id` = ?", array($messageId));
         $userIdArr = array();
         foreach ($userArr as $v) {
             $userIdArr[] = $v['id'];
         }
         $sql = "SELECT `id`,`devicetoken`,`client_id` FROM `" . $userDB->getTableName() . "` WHERE `id` IN ?";
         $userArr = $userDB->getData($sql, array($userIdArr));
     } else {
         $userToArr = explode(",", $_GET['to']);
         $sql = "SELECT `id`,`devicetoken`,`client_id` FROM `" . $userDB->getTableName() . "` WHERE `username` IN ?";
         $userArr = $userDB->getData($sql, array($userToArr));
     }
     if (empty($userArr)) {
         Message::showError('user is invalid', 15);
     }
     if (empty($_GET['content'])) {
         Message::showError('content not allow empty', 12);
     }
     if (empty($_GET['level'])) {
         $_GET['level'] = 1;
     }
     if (empty($_GET['session'])) {
         $userArr = Common::arrayOrderBy($userArr, 'id');
         $uidStr = '';
         foreach ($userArr as $v) {
             $uidStr .= $v['id'];
         }
         $session = md5($uidStr);
     } else {
         $session = $_GET['session'];
     }
     if (empty($_GET['from'])) {
         $sendUser = 0;
     } else {
         $sql = "SELECT `id` FROM `" . $userDB->getTableName() . "` WHERE `username` = ?";
         $userId = $userDB->getFirst($sql, array($_GET['from']));
         if (!empty($userId)) {
             $sendUser = $userId;
         } else {
             $sendUser = -1;
         }
     }
     $insertArr = array();
     $insertArr['message'] = $_GET['content'];
     $insertArr['level'] = $_GET['level'];
     $insertArr['session'] = $session;
     $insertArr['send_user'] = $sendUser;
     $insertArr['time'] = time();
     $rs = $messageDB->insert($insertArr);
     if (empty($rs)) {
         Message::showError('insert message error', 13);
     }
     $messageId = $messageDB->insertId();
     $token = "";
     foreach ($userArr as $user) {
         $insertArr = array();
         $insertArr['user_id'] = $user['id'];
         $insertArr['message_id'] = $messageId;
         $insertArr['send_user'] = $sendUser;
         $rs = $listDB->insert($insertArr);
         if (empty($rs)) {
             Message::showError('insert message error!!!', 14);
         }
         if ($_GET['level'] == 3) {
             $sound = "3.caf";
         } elseif ($_GET['level'] == 2) {
             $sound = "2.caf";
         } else {
             $sound = "1.caf";
         }
         if ($sendUser != $user['id']) {
             if (!empty($user['devicetoken'])) {
                 $token .= $user['devicetoken'] . ",";
             }
         }
         $clientIds[] = $user['client_id'];
         //用户发送信息, 就是发送推送.
     }
     if ($sendUser > 0) {
         $from = $_GET['from'];
         $content = $from . ":" . $_GET['content'];
     } else {
         $from = "";
         $content = $_GET['content'];
     }
     Http::post("http://i.client.mix.sina.com.cn/api/push/send", array("appid" => "6486b4e9-8228-b83d-4b13-d54f-ebe5f170", "platform" => "apns", "apns_token" => $token, "title" => $content, "sound" => $sound, "badeg" => 0, "otherdata" => '{"from":"' . $from . '"}', "debug" => 1), array("referer" => "sina.com.cn"), 10);
     pushNotificationMessageToList('aha', $_GET['level'] . '#' . $content, $clientIds);
     Message::showSucc('succ', $showArr);
 }