PubNub:发布在3G网络上似乎不适合我

时间:2016-01-27 08:59:04

标签: publish-subscribe pubnub

我正在使用PubNub。基本上发布和订阅没有问题。但是,发布不适用于3G网络等慢速连接。 Wifi还可以,但有人说4G也有同样的问题。有谁知道如何解决它?

我想做的是在下面。

void TestScene::onTouchMoved(Touch * touch, Event* event)
{
    Vec2 start = touch->getLocation();
    Vec2 end   = touch->getPreviousLocation();

    // begin drawing to the render texture
    _renderTexture->begin();

    float distance = start.getDistance(end);
    if (distance > 1)
    {
        Sprite * brushSprite = Sprite::create("Images/fire.png");
        brushSprite->setColor(Color3B::RED);
        brushSprite->setOpacity(20);

        for (int i = 0; i < distance; i++)
        {
            float difx = end.x - start.x;
            float dify = end.y - start.y;
            float delta = (float)i / distance;
            brushSprite->setPosition(start.x + (difx * delta), start.y + (dify * delta));
            brushSprite->visit(); // Draw sprite to render texture
        }
    }

    // finish drawing and return context back to the screen
    _renderTexture->end();
}

1 个答案:

答案 0 :(得分:3)

3G / 4G / LTE移动网络上的PubNub

调用backfill: true方法时使用pubnub.subscribe(...)选项。这允许您的设备在移动网络上接收消息。回填选项可防止移动网络延迟竞争条件。

// Setup
var channel = 'a'+Math.random();
var pubnub  = PUBNUB({
    ssl             : true
,   "publish_key"   : "pub-c-aefb421c-b30a-4afc-bae4-b866c5ea3d69"
,   "subscribe_key" : "sub-c-76f89e66-c3a9-11e5-b5a8-0693d8625082"
});

// Receive Message
pubnub.subscribe({
    backfill : true
,   channel  : channel
,   error    : out
,   connect  : publish
,   message  : out
});

// Send Message
function publish() { 
    out("CONNECTED!");
    pubnub.publish({
        channel : channel
    ,   error   : out
    ,   message : "SUCCESS IT WORKS!"
    });
}

// Network Check
pubnub.time(function(a){out(a ? "NETWORK CHECK" : "NETWORK BAD")});

function out(m) {
    document.getElementById("result").innerHTML 
        += "<br>" + JSON.stringify(m); 
}
<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
<h1>PubNub 3G/4G/LTE Network</h1>
<div id="result">PROCESSING_CONNECTIVITY...</div>

PubNub Mobile Network 3G 4G LTE

  

PubNub移动网络 - 示例输出

您也可能决定要其他选项。您还可以在pubnub.publish()回调中发出connect方法调用。

pubnub.subscribe({
    channel  : channel,
    message  : out,
    connect  : function() {
        pubnub.publish({
            channel : channel
        ,   message : "SUCCESS IT WORKS!"
        });
    }
});