2012-08-16 79 views
3

我已經創建了簡單的聊天應用程序。並因此我已經使用node.js 我已經看到了許多簡單的例子在網上,所有人都說,代碼工作正常。 但是當我嘗試的代碼,它不給我適當的結果。node.js中的socket.io redis pub/sub

它拋出錯誤 「始亂終棄運輸」

我已閱讀以下頁面: 1)Examples in using RedisStore in socket.io 2)http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html 3)socket.io broadcast function & Redis pub/sub architecture 4)I'm receiving duplicate messages in my clustered node.js/socket.io/redis pub/sub application 還有更多...

以下是我的代碼:

服務器端代碼:app.js ~~~~~~~~~~~~~~~~~~~~~~

var app = express.createServer(); 
app.listen(process.env.PORT); 

var io = require('socket.io').listen(app); 

var store = redis.createClient(); 
var pub = redis.createClient(); 
var sub = redis.createClient(); 

var io = require('socket.io').listen(app); 

io.configure(function() { 

    // io.enable('browser client minification'); // send minified client 
    // io.enable('browser client etag');   // apply etag caching logic based on version number 
    // io.enable('browser client gzip');   // gzip the file 

    io.set('log level', 3); 
    io.set("transports", ["jsonp-polling", "xhr-polling", "websocket", "flashsocket", "htmlfile"]); 
    io.set("polling duration", 10); 
    io.set("flash policy server", false); 
    io.set("connect timeout", 500); 
    io.set("reconnect", true); 
    // io.set('close timeout', 60 * 60 * 24); // 24h time out 
    io.set('close timeout', 25); 
    io.disable('heartbeats'); 
    io.set('heartbeat interval', 20); 
    io.set('heartbeat timeout', 60); 
    // io.set("polling duration", 10); 
    // io.set("heartbate timeout", 30); 
    //console.log("blabla"); 

    //var RedisStore = require('socket.io/lib/stores/redis'); 
    //io.set('store', new RedisStore({ redisPub: pub, redisSub: sub, redisClient: store })); 
    //io.set('store', new RedisStore()); 
}); 

io.sockets.on('connection', function (client) { 

    client.on("OnConnect", function (data, fn) { 
     console.log("socket id : " + client.id + " connected !!!"); 
    }); 

    client.on('disconnect', function() { 
     console.log("\r\nmanish from server->disconnect"); 
     //  client.broadcast(client.sessionId + " disconnected") 
     client.emit('user disconnected'); 
     sub.unsubscribe("chat"); 
     sub.quit(); 
    }); 

    sub.subscribe("chat"); 
    sub.on("message", function (channel, message) { 
     console.log("message received on server from publish : '" + message + "'"); 
     client.send(message); 
    }); 
    }); 
}); 

客戶端代碼:index.html的 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 this.Connect = function (nick, room) { 
      socket = io.connect('http://XXX.XXX.X.XX', { transports: ['jsonp-polling', 'xhr-polling'] }); 
      Nickname = nick; 
      Room = room; 

//   setInterval(function() { socket.emit("keep-alive", null) }, 20 * 1000); 

      socket.on('connect', function (data) { 
       socket.emit('OnConnect', { nick: nick, room: room }, function (response) { 
        $("#board").append("<p>" + response.msg + "</p>"); 
       }); 
      }); 

      socket.on("message", function (msg) { 
       alert("message received on client ..."); 
       $("#board").append("<p>" + msg +"</p>"); 
      }); 

      server.on("listening", function() { 
       var address = server.address(); 
       console.log("server listening " + address.address + ":" + address.port); 
      }); 

      socket.emit("message", { msg: msg, nick: Nickname }, function (response) { 
       $("#board").append("<p> send message : " + Nickname + ": " + msg + "</p>"); 
      }); 

     }; 

~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~但我不適合我。

意味着我在一個瀏覽器中運行這個程序(讓說,Firefox和發送一些消息,第二連接,讓說IE)

,但節目繼的console.log

調試錯誤:設置請求GET /socket.io/1/jsonp-polling/Th9U-Zci8cVb5Wfwl24Y?t=1345123231804 & I = 0 調試:設置輪詢超時 調試:客戶端授權 調試:具有清熱調查超時 調試:jsonppolling寫IO。 J0; 調試:爲客戶端設置關閉超時Th9U-Zci8cVb5Wfwl24Y 套接字ID:Th9U-Zci8cVb5Wfwl24Y連接!從發佈服務器上接收 消息: 'MSG 1' 調試:設定請求GET /socket.io/1/jsonp-polling/Th9U-Zci8cVb5Wfwl24Y?t=1345123231804 & I = 0 調試:設置輪詢超時 調試:客戶端授權 調試:清除輪詢超時 調試:jsonppolling寫入io.j0; 調試:爲客戶端設置關閉超時Th9U-Zci8cVb5Wfwl24Y 套接字ID:Th9U-Zci8cVb5Wfwl24Y連接! 「味精1」 調試:從發佈服務器接收 消息解僱接近超時客戶Th9U-Zci8cVb5Wfwl24Y 信息:運輸結束(接近超時) 調試:丟棄運輸

回答

2

Socket.io房間裏工作的開箱你不需要訂閱redis或任何東西。

嘗試在您的自定義連接功能

client.on("OnConnect", function (data, fn) { 
    console.log("socket id : " + client.id + " connected !!!"); 
    client.join(data.room); 
}); 

而且你不需要從房間退訂,但如果你這樣做應該是這樣的

client.on('disconnect', function() { 
    console.log("\r\nmanish from server->disconnect"); 
    client.emit('user disconnected'); 
    client.leave("chat"); 
}); 

發送消息到聊天室是通過 client.broadcast.to('chat')。emit('message');

您可以在https://github.com/LearnBoost/socket.io/wiki/Rooms

好運閱讀更多關於房間!