我在我的聊天应用程序中使用 socket.io 来显示用户,但几个月后我发现了很多关于防火墙、CPU 性能和内存的问题,我听到了很多关于 sockjs 的好见解,我想知道如何从套接字转换这些代码.io 到 sockjs 或者如果您有任何其他技术而不是 socket.io 或 sockjs
var app = require('express')(),
redis = require('redis'),
client = redis.createClient(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(844);
io.enable('browser client minification'); // send minified client
io.enable('browser client etag');
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [
' websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling'
]);
io.sockets.on('connection', function (socket) {
socket.on('join', function (userid, hello) {
socket.set('userid', userid);
socket.userId = userid;
var userco = userid + ":connects";
var usercostat = userid + ":stats";
var userhashy = userid + ":userhash";
client.get(userhashy, function (err, replay) {
if (replay === hello) {
console.log("tam el tatabok");
client.get(usercostat, function (err, reply) {
if (reply !== "only me") {
console.log("not only me ");
client.incr(userco);
io.sockets.emit("add-user", {
id: userid
});
} else {
console.log("only me ");
io.sockets.emit("remove-user", {
id: socket.userId
});
}
});
} else {
return false;
}
});
});
socket.on('disconnect', function (userid) {
socket.get('userid', function (err, userid) {
var userco = userid + ":connects";
client.get(userco, function (err, reply) {
if (reply > 0) {
console.log("akbar men 0");
client.decr(userco);
}
});
setTimeout(function () {
client.get(userco, function (err, reply) {
if (reply < 1) {
console.log("hanshelo");
io.sockets.emit("remove-user", {
id: socket.userId
});
}
});
}, 2000);
});
});
});
当我迁移到 sockjs 时遇到的主要问题是我需要有 socket.set 和 socket.get 之类的东西。我有用户 ID,以便在用户打开或关闭连接时可以使用它