PubNub - REST Api usage

Hi folks,

if you consider using http://www.pubnub.com for your network needs, here is a small script to show how to use http requests for it:

Cerberus X:
Strict

' PubNub.Com example of their REST API

Import mojo
Import brl.httprequest

'-----------------------------------------------------------------
Class myClass Extends App Implements IOnHttpRequestComplete

    Field get_req:HttpRequest

    '-----------------------------------------------------------------
    Method OnHttpRequestComplete:Void( req:HttpRequest )

        If req=get_req
            Print "Http GET complete!"
        Endif

        Print "Status="+req.Status()
        Print "ResponseText="+req.ResponseText()
   
    End
    '-----------------------------------------------------------------
    Method OnCreate:Int()
        SetUpdateRate 60
   
   
        'Buildung up the Pubnub string

        'Publish
        Local pubKey:String = "demo"
        Local subKey:String = "demo"
        Local signature:String = "0"
        Local channel:String = "hello_world"
        Local callback:String = "0"
        Local message:String = "%22Hello%20World777%22"
   
        Local pubnubP:String = "publish/"   + pubKey + "/" + subKey  + "/" + signature + "/" + channel + "/" + callback + "/" + message
   
        'History
        Local limit:String = "3"
        Local pubnubH:String = "history/"   + subKey + "/" + channel + "/" + callback  + "/" + limit
   
        'Subscribe
        Local timeToken:String = "0"
        Local pubnubS:String = "subscribe/" + subKey + "/" + channel + "/" + callback  + "/" + timeToken

        get_req=New HttpRequest( "GET","http://pubsub.pubnub.com/"+pubnubH,Self )
        get_req.Send

        Return 0
    End
    '-----------------------------------------------------------------
    Method OnUpdate:Int()
        If KeyHit( KEY_CLOSE ) Error ""

        UpdateAsyncEvents()
        Return 0
    End
    '-----------------------------------------------------------------
    Method OnRender:Int()
        ' here you render your app
        Cls 0,0,128
        SetColor 128,128,128
        DrawText "Http GET bytes received="+get_req.BytesReceived(),0,0
        Return 0
    End
End

'-----------------------------------------------------------------
Function Main:Int()
    New myClass
    Return 0
End
 
Last edited:

Comments

Look good and working. When I test with my website the responsetext is null. Could you help me understand this thing...

I use monkey 79e currently and plan to port to lates CerberusX. In my code, I send and receive data using http request. It's all working before. The link is: ~EDIT~

That for my game high score. I process the string that I receive to make a table.
 
Last edited:
To make this clear. It worked at some time.... and now does not work anymore? Your own code and website?

This code above is for Pubnub. It would never work on your site.
 
> It worked at some time.... and now does not work anymore?

I't still work on MX 79e. But not work on CX. What I mean is, If I use the above code and replace with my http address above, its work in MX but in CX the response text return Null. In MX I get the responsetext. (The responsetext is exactly the same when I open in browser).

> Your own code and website?

Yes. It is a simple php with sql to record my user highscore. From my game I use httprequest to send data to update highscore. the php send back current highscore in just a text. I then process the string to make a proper gui. Its a stupid temporary solution actually...because at that time I cannot integrated googlegameservice and I need a highscore system.

Sorry if this is so stupid. ;)
But on old MX I can read responsetext() from that http address. I don't know, not an expert on this area. Is there any thing I should do on server to allow client reading httprequest? Suppose, we should be a able to get httprequest from any address right?
 
To my knowledge, on the HTML target we didn't change anything.
MX 79e is super old, MX was on 87f the last I think.
I have to investigate. As you don't provide any code and stuff I can try, I will need time to build something.
 
| As you don't provide any code and stuff I can try, I will need time to build something

You can use the above code and replace with my http address.
 
So it is a bug.. https://www.cerberus-x.com/community/threads/http-get-onhttprequestcomplete.395/

Btw... I delete my http link that I provide earlier. No use anymore. Thank you @Mike for taking time to examine it
I am not sure about it anymore. I have installed Monkey 79d and it does not return anything with your webadress on HTMl5 but with PubNub it does with my code
Regarding the bug I suspected.... It could be a time out which we introduced and should be set higher in CX.
But with Monkey it doesn't have that...


So please provide code that runs in Monkey 79d but not in CX.
 
This sample code for reconstruct the error:
Compile android MX 79e = Run OK
Compile android CX 20180302 = App terminate
i'm not sure why.. could it be my site?

Code:
Strict
Import mojo
Import brl.httprequest
Class myClass Extends App Implements IOnHttpRequestComplete
    Field get_req:HttpRequest
     Field ResponseText:String
    Method OnHttpRequestComplete:Void( req:HttpRequest )
        If req=get_req
              ResponseText =req.ResponseText()
        Endif
    End
    Method OnCreate:Int()
        SetUpdateRate 60
        get_req=New HttpRequest( "GET","http://nvgamepad.com/temp/highscore.php",Self )
        get_req.Send
        Return 0
    End
    Method OnUpdate:Int()
        If KeyHit( KEY_CLOSE ) Error ""
        UpdateAsyncEvents()
        Return 0
    End
    Method OnRender:Int()
        PushMatrix()
              Scale (3,3)
              Cls 0,0,0
              SetColor 255,255,255
              DrawText "Http Request:",0,0   
              If ResponseText=""
                    DrawText "Waiting..",0,50   
              Else
                    DrawText "Response Text received:",0,25   
                    DrawText ResponseText,0,50   
              Endif
        PopMatrix()
        Return 0
    End
End
Function Main:Int()
    New myClass
    Return 0
End
 
Top Bottom