用户与sockjs的存在

时间:2013-11-08 23:10:03

标签: node.js socket.io sockjs

我在聊天应用程序中使用socket.io来存在用户,但几个月后我发现了许多关于防火墙,CPU性能和内存的问题,我听到很多关于sockjs的好见解,我想知道如何转换这些从socket.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所以我可以在用户打开或关闭连接时使用它

0 个答案:

没有答案