fra*_*erm 2 node.js express socket.io
我目前正在寻找一种替代方案来使用 socket.io 扩展我的 Express 应用程序。问题是我不想使用 redis 作为 socket.io 存储。除了Clusterhub之外,还有其他可能性来集群 socket.io 吗?
编辑:我尝试使用fakeredis作为 redis 的替代品,但它似乎不适用于 socket.io。从ActionHero.js我知道 faye-websocket 可以与 fakeredis 配合使用。
这很可能取决于您的 socket.io 使用情况以及您想要实现的扩展类型(集群与扩展到多台机器)。
因此,这就是我将 socket.io 的使用扩展到多个服务器所做的事情。
我们在负载均衡器后面有 3 个服务器,当套接字连接时,它会连接到 3 个服务器中的任何一个,这三个服务器在内存中有一个套接字列表,并且这三个服务器有一个内部服务器地址的顺序列表,例如 [server1,服务器2,服务器3]。
我所做的基本上是一个环(在内部我们称之为“套接字环”):
我用来确定下一个节点(next_node.js)的算法是:
var nodes = process.env.NODES.split(',');
//this is usually: http://server1/,http://server2/,http://server3/
var url = require('url');
var current = require("os").hostname();
//origin is the node that started the lookup
exports.get = function (origin) {
var next_node_i = nodes.map(function (uri) {
return url.parse(uri).hostname;
}).reduce(function (prev, curr, i, arr){
return curr === current && i < arr.length - 1 ? i + 1 : prev;
}, 0);
var next_node = nodes[next_node_i];
if (origin && url.parse(next_node).hostname === origin) {
// if the next node is equal to the first node initiating the lookup
// it means the socket we are looking for is not connect to any node.
return null;
}
return next_node;
};
Run Code Online (Sandbox Code Playgroud)
注意事项:
在我看来,这是在许多 socket.io 用例中实现扩展的一种非常简单的方法,可能在很多其他场景中这不是一个选项,但我希望这能提供一些想法。
归档时间: |
|
查看次数: |
2743 次 |
最近记录: |