heroku上的socket io

时间:2013-04-09 21:17:32

标签: node.js heroku socket.io

我在heroku上有一个简单的节点程序,它定期向所有客户端发送一条消息。但我一直收到下面的警告,导致重新连接。这是我应该担心的吗?我每2-3次发射就得到这个。

要注意,我在localhost上没有收到任何警告或重新连接。我正在使用本地和heroku实例的长轮询配置。此外,我在heroku上扩展我的PS越高,我得到的警告越多。

错误

warn: client not handshaken client should reconnect

app.js

io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});

io.sockets.on('connection', function (socket) {
  socket.emit('news', 'reconnect');
});

setInterval(function(){
  io.sockets.emit('news', {time: new Date()} )
},3000);

client.js

var socket = io.connect('/');
socket.on('news', function (data) {
  console.log(data);
});

1 个答案:

答案 0 :(得分:2)

  

此外,我在heroku上扩展我的PS越高,我得到的警告越多。

是关键。

Socket.io旨在在单个进程中运行。它将会话存储在内存中。你需要你的代理是粘性的,总是将同一个用户发送到同一个进程,或者你需要共享会话(从那以后你共享会议室就好了);

信息是@

https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

缺点是你需要使用redis

var RedisStore = require('socket.io/lib/stores/redis')
  , redis  = require('socket.io/node_modules/redis')
  , pub    = redis.createClient()
  , sub    = redis.createClient()
  , client = redis.createClient();

io.set('store', new RedisStore({
  redisPub : pub
, redisSub : sub
, redisClient : client
}));

幸运的是,heroku有五个不同的redis插件,其中三个有免费层。