Esempio n. 1
0
 $innerHttpWorker = new Worker('http://0.0.0.0:' . SERVER_API_PORT);
 // On msg receiving
 $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);
Esempio n. 2
0
});
// 当$sender_io启动后监听一个http端口,通过这个端口可以给任意uid或者所有uid推送数据
$sender_io->on('workerStart', function () {
    // 监听一个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);