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