コード例 #1
0
ファイル: start.php プロジェクト: qiushuiqs/RocketPush
 $innerHttpWorker->onMessage = function ($httpConnection, $data) {
     if (!isset($_REQUEST) && count($_REQUEST) <= 0) {
         return $httpConnection->send(json_encode(array("result" => false)));
     }
     // Send msg url like: "type=sendTextMsg&sendTo=xxxx&sendContent=xxxx"
     switch ($_REQUEST['type']) {
         case 'sendTextMsg':
             global $senderIO;
             if (isset($_REQUEST['sendTo']) && !empty($_REQUEST['sendTo'])) {
                 $SendTo = $_REQUEST['sendTo'];
             }
             $SendContent = json_encode(array("data" => htmlspecialchars($_REQUEST['sendContent'])));
             if (!empty($SendTo)) {
                 $senderIO->to($SendTo)->emit('new_msg_receive', $SendContent);
             } else {
                 $senderIO->emit('new_msg_receive', $SendContent);
             }
             // return status
             return $httpConnection->send(json_encode(array("result" => true)));
     }
     return $httpConnection->send(json_encode(array("result" => false)));
 };
 // Start listening
 $innerHttpWorker->listen();
 // Init a timer
 Timer::add(1, function () {
     global $uidConnectionMap, $senderIO, $lastOnlineCount, $lastOnlinePageCount;
     $onlineCountNow = count($uidConnectionMap);
     $onlinePageCountNow = array_sum($uidConnectionMap);
     // Send updated data when client counts changes
     if ($lastOnlineCount != $onlineCountNow || $lastOnlinePageCount != $onlinePageCountNow) {
コード例 #2
0
ファイル: start.php プロジェクト: joy696163/web-msg-sender
 // 监听一个http端口
 $inner_http_worker = new Worker('http://0.0.0.0:2121');
 // 当http客户端发来数据时触发
 $inner_http_worker->onMessage = function ($http_connection, $data) {
     $_POST = $_POST ? $_POST : $_GET;
     // 推送数据的url格式 type=publish&to=uid&content=xxxx
     switch (@$_POST['type']) {
         case 'publish':
             global $sender_io;
             $to = @$_POST['to'];
             // 有指定uid则向uid所在socket组发送数据
             if ($to) {
                 $sender_io->to($to)->emit('new_msg', @$_POST['content']);
                 // 否则向所有uid推送数据
             } else {
                 $sender_io->emit('new_msg', @$_POST['content']);
             }
             // http接口返回ok
             return $http_connection->send('ok');
     }
     return $http_connection->send('fail');
 };
 // 执行监听
 $inner_http_worker->listen();
 // 一个定时器,定时向所有uid推送当前uid在线数及在线页面数
 Timer::add(1, function () {
     global $uidConnectionMap, $sender_io, $last_online_count, $last_online_page_count;
     $online_count_now = count($uidConnectionMap);
     $online_page_count_now = array_sum($uidConnectionMap);
     // 只有在客户端在线数变化了才广播,减少不必要的客户端通讯
     if ($last_online_count != $online_count_now || $last_online_page_count != $online_page_count_now) {