gpt4 book ai didi

node.js - sticky-session 与 socket.io-sticky-session Node js 库!哪个更好?

转载 作者:搜寻专家 更新时间:2023-10-31 23:46:57 30 4
gpt4 key购买 nike

我遇到过这两个用于 node js 的粘性 session 库

https://github.com/indutny/sticky-session

https://github.com/wzrdtales/socket-io-sticky-session

两者有什么区别,我的需求只是实现 Node 集群的socket,以后如果要添加Ngnx Server。

在 Socket.io 文档中他们提到了前一个,但是这个链接

Socket.io 'Handshake' failing with cluster and sticky-session

说第二个更好!

最佳答案

最近,我发现sticky-cluster这个模块的实现非常简单。

基准测试非常好,他们的描述说:sticky-session 模块快 10 倍,散射也好得多

示例代码:

'use strict';
var sticky = require('sticky-cluster');

function startFn (callback) {
var async = require('async');
async.waterfall(
[

// connect to remote services

function (callback) {
async.parallel(
[
// fake db 1
function (callback) { setTimeout(callback, 2000); },

// fake db 2
function (callback) { setTimeout(callback, 1000); }
],
callback
);
},

// configure the worker

function (services, callback) {
var http = require('http');
var app = require('express')();
var server = http.createServer(app);

// get remote services
//var fakedb1 = services[0];
//var fakedb2 = services[1];

// all express-related stuff goes here, e.g.
app.use(function (req, res) { res.end('Handled by PID = ' + process.pid); });

// all socket.io stuff goes here
//var io = require('socket.io')(server);

// don't do server.listen(...)!
// just pass the server instance to the final async's callback
callback(null, server);
}

],
function (err, server) {

// fail on error
if (err) { console.log(err); process.exit(1); }

// pass server instance to sticky-cluster
else callback(server);
}
);
}

sticky(startFn, {
concurrency: parseInt(require('os').cpus().length, 10),
port: parseInt(process.env.PORT, 10),
debug: (process.env.NODE_ENV === 'development')
});

关于node.js - sticky-session 与 socket.io-sticky-session Node js 库!哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454461/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com