node,pubnub,socket.io的基本用法不起作用

时间:2016-06-30 08:07:34

标签: pubnub alexa-skills-kit alexa-skill

我正在尝试将socket.io与pubnub一起使用。我无法理解如何在客户端(index.html)上获取我在服务器上发布的数据.Below是示例代码。

我的server.js

var pubnub = require("pubnub")

var p = pubnub.init({
  "subscribe_key" : "xxxx",
 "publish_key" : "xxxx",
 "params" : {},
});

 p.publish({
  "message" : "foo",
  "channel" : "test_channel",
 });

客户端代码 - index.html

  <script src="http://cdn.pubnub.com/socket.io.min.js"></script>
  <script>(function(){
   // IMPORTANT: PubNub Setup with Account
   var pubnub_setup = {
      channel       : 'test_channel',
      publish_key   : 'xxxx',
     subscribe_key : 'xxxx'
     };

   var socket = io.connect( 'http://pubsub.pubnub.com/', pubnub_setup );

   socket.on( 'connect', function() {
    console.log('Connection Established! Ready to send/receive data!');
   } );

   socket.on( 'message', function(message) {
    console.log(message);
   } );

   socket.on( 'disconnect', function() {
    console.log('my connection dropped');
   } );


   socket.on( 'reconnect', function() {
    console.log('my connection has been restored!');
   } );

 })();</script>

1 个答案:

答案 0 :(得分:0)

PubNub Socket.io SDK

other Stack Overflow thread, the PubNub Socket.IO SDK for PubNub is designed for people that started with socket.io but want to migrate to PubNub所述。否则,如果您首先使用PubNub,则不需要使用Socket.IO SDK,您应该使用最新的PubNub Node SDKPubNub JavaScript SDKwhatever PubNub SDK you require

新实施的符合ES5的PubNub JavaScript/Node v4 SDKs (currently in beta)即将推出!