訂閱成功釋出(連線)

此示例顯示如何訂閱,一旦成功,就會向該頻道釋出訊息。它還演示了可以包含在 subscribemessage 回撥函式中的全套引數。

pubnub = PUBNUB({                          
    publish_key   : 'your_pub_key',
    subscribe_key : 'your_sub_key'
});

pubnub.subscribe({                                     
    channel : "channel-1",
    message : function (message, envelope, channelOrGroup, time, channel) {
        console.log(
        "Message Received." + "\n" +
        "Channel or Group: " + JSON.stringify(channelOrGroup) + "\n" +
        "Channel: " + JSON.stringify(channel) + "\n" +
        "Message: " + JSON.stringify(message) + "\n" +
        "Time: " + time + "\n" +
        "Raw Envelope: " + JSON.stringify(envelope)
    )},
    connect:    pub,
    disconnect: function(m) {console.log("DISCONNECT: " + m)},
    reconnect:  function(m) {console.log("RECONNECT: " + m)},
    error:      function(m) {console.log("ERROR: " + m)}
});

function pub() {
   pubnub.publish({                                    
        channel : "channel-1",
        message : {"msg": "I'm Puuumped!"},
        callback: function(m) {console.log("Publish SUCCESS: " + m)},
        error: function(m) {console.log("Publish ERROR: " + m)}
   })
};