2012-11-24 4 views
0

express 3x, node.js 및 redis를 사용하고 있습니다. 메시지를 게시 할 때 1 번은 구독시 2-3 번 메시지를받습니다. (예 : 브라우저를 새로 고칠 때마다 매회 메시지 수신이 1 증가합니다). 아래 코드 이 내 코드입니다.중복 메시지 수신시 중복 메시지가 수신되었습니다.


서버 측 : ~~~~~~~~~~

var express = require('express'), 
    http = require('http') 

var redis = require('redis'); 
var redisCli = redis.createClient(); 
var redisPub = redis.createClient(); 
var redisSub = redis.createClient(); 

redisCli.on("error", function (err) { 
    console.error("\r\n Error generated from redis client ", err); 
}); 
redisPub.on("error", function (err) { 
    console.error("\r\n Error generated from redisPub ", err); 
}); 
redisSub.on("error", function (err) { 
    console.error("\r\n Error generated from redisSub ", err); 
}); 

var server = http.createServer(app) 
    , io = require('socket.io').listen(server); 

server.listen(process.env.PORT); 

app.configure(function() { 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.set('view options', { layout: false }); 

    app.use(express.favicon(__dirname + '/favicon.ico', { maxAge: 2592000000 })); 
    app.use(express.bodyParser()); 
    app.use(express.cookieParser()); 
    app.use(express.session({ secret: "myKey", store: new RedisStore({ maxAge: 86400000, client: redisCli }), cookie: { maxAge: 86400000} })); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/static')); 
}); 

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', 1); 
    io.set("flash policy server", false); 
    io.set("transports", ["jsonp-polling", "xhr-polling"]); 
}); 

io.sockets.on('connection', function (client) { 
    console.log("server - redisSub.subscribe from io.on.connection"); 
    redisSub.unsubscribe(); 
    redisSub.subscribe("announcement"); 

    redisSub.on("message", function (channel, message) { 
     io.sockets.emit('announcement', message); 
    }); 

    client.on('disconnect', function() { 
     redisSub.unsubscribe("announcement"); 
     redisSub.quit(); 
    }); 

}); 

app.post('/PublishMessage', function (req, res) { 
    redisPub.publish("announcement", req.body.users); 

    res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store'); 
    res.setHeader('Connection', 'keep-alive'); 
    res.contentType('application/json'); 
    res.setHeader('Expires', new Date().addYears(-10)); 
    res.json({ result: 'ok' }); 
}); 

클라이언트 측 ~~~~~~~~~

this.socket = io.connect('http://XXX.XXX.X.XXX/', { transports: ['jsonp-polling', 'xhr-polling'] }); 
    this.socket.on('connect', function() { 
     alert("client - Socket client connect"); 
    }); 
    this.socket.on('announcement', function (msg) { 
     alert("clientside - announcement "); 
     var nUsers = parseInt($('#Summary>article>p:last').text(), 10) + parseInt(msg, 10); 
     $('#Summary>article>p:last').text(nUsers); 
    }); 

========================================================================================================== =============== 그래서 어느 누구도 같은 가이드! 대단히 감사합니다.

답변

6

나는 socket.io를 한번도 사용하지 않았지만 연결 처리기로 복잡한 작업을하는 것처럼 보입니다.

핸들러 내부에서 어떤 방식 으로든 (사용자가 연결 한 이벤트를 내보내는 것과 같이) 연결에 반응하거나 개별 소켓 연결의 동작을 수정하는 것처럼 보이지 않습니다.

입니다. redisSub 클라이언트를 반복적으로 구독하고 구독을 취소하고 있습니다. 나는 여기서 잘못 될 수는 있지만, 그렇게 할 필요가 없거나 그렇게해야한다고 생각하지 않습니다.

모든 연결에서이 글로벌 클라이언트를 서브/미사용 할 필요가 없기 때문에 "announcement"를 연결 처리기의 외부로 한 번 서브 페이지로 서브 커넥션 처리해야합니다.. Like :

// Move this subscription outside of the connection handler, and you shouldn't 
// have to continue to sub/unsub or otherwise manage it. 
redisSub.on("message", function (channel, message) { 
    io.sockets.emit('announcement', message); 
}); 

// Since you're not reacting to connections or doing anything with individual 
// connection sockets, you don't really have anything to do in this handler. 
io.sockets.on('connection', function (socket) { 
    // if you ONLY wanted to emit to this socket, you'd do it here 
    //socket.emit("announcement", "just for this connection") 
}); 
+0

이것은 나를 위해 일했습니다. 여기에서 Addy의 예제를 사용 했으므로 https://github.com/addyosmani/backbone-boilerplates/blob/master/option7_node_socketio/sockets/index.js#L82에서 간과했다. 해당 코드와 동일한 메시지 동작이 중복됩니다. 그러나 연결 처리기에서 sub.on ("message") 콜백 만 이동했습니다. 연결된 클라이언트를 채널에 실제로 등록해야하기 때문에 구독 처리 부분을 연결 처리기 안에 남겨 두었습니다. 수신 거부는 연결 해제시 발생합니다. 나는 틀린 tho 일 수 있었다. – agwntr

+0

오른쪽 자리 숫자 1311407 –

+0

감사합니다. 선생님, 방금이 주석 줄을 도와 주셨습니다. "//이 구독을 연결 처리기 외부로 이동하면" – felipekm