2012-03-09 2 views
2

방을 가로 질러 메시지를 보내려고합니다. 내가 가진 :크로스 룸 통신 퍼즐

server.js

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

var chat = io 
    .of('/chat') 
    .on('connection', function (socket) { 
    socket.on('chatTestEmit1', function(d) { 
     console.log('>> chatTestEmit1 >> '+JSON.stringify(d)); 
     console.log(io.sockets.manager.rooms); 
     socket.to('/news').emit('newsTestEmit1', d); 
    }); 
    }); 

var news = io 
    .of('/news') 
    .on('connection', function (socket) { 
    socket.on('checkAlive', function(d) { 
     console.log('>> checkAlive >> '+JSON.stringify(d)); 
    }); 
    socket.on('newsTestEmit1', function(d){ 
     console.log('>> newsTestEmit1 >> '+JSON.stringify(d)); 
    }); 
    }); 

client.html

<script src="http://localhost:8081/socket.io/socket.io.js"></script> 
<script> 
    var chat = io.connect('http://localhost:8081/chat') 
    , news = io.connect('http://localhost:8081/news'); 

    chat.on('connect', function() { 
    chat.emit('chatTestEmit1',{msg:'This is the message from client.'}); 
    }); 

    news.on('connect', function(){ 
    news.emit('checkAlive',{msg:'News is alive'}); 
    }); 
</script> 

로그는 다음과 같습니다 :이 코드가 제대로 작동하면

info - socket.io started 
    debug - client authorized 
    info - handshake authorized 74858017692628057 
    debug - setting request GET /socket.io/1/websocket/74858017692628057 
    debug - set heartbeat interval for client 74858017692628057 
    debug - client authorized for 
    debug - websocket writing 1:: 
    debug - client authorized for /chat 
    debug - websocket writing 1::/chat 
    debug - client authorized for /news 
    debug - websocket writing 1::/news 
>> chatTestEmit1 >> {"msg":"This is the message from client."} 
{ '': [ '74858017692628057' ], 
    '/chat': [ '74858017692628057' ], 
    '/news': [ '74858017692628057' ] } 
    debug - websocket writing 5::/chat:{"name":"newsTestEmit1","args":[{"msg":"This is the message from client."}]} 
>> checkAlive >> {"msg":"News is alive"} 
    debug - emitting heartbeat for client 74858017692628057 
    debug - websocket writing 2:: 
    debug - set heartbeat timeout for client 74858017692628057 
    debug - got heartbeat packet 
    debug - cleared heartbeat timeout for client 74858017692628057 
    debug - set heartbeat interval for client 74858017692628057 
    debug - emitting heartbeat for client 74858017692628057 

, 그것은 로그인 상태에서만 가능합니다 아래와 같이 :

>> newsTestEmit1 >> {"msg":"This is the message from client."} 

이 로그에 나타납니다. 나는 또한 시도했다 :

io.sockets.in('/news').emit('newsTestEmit1', d); 
io.sockets.in('news').emit('newsTestEmit1', d); 
io.of('/news').emit('newsTestEmit1', d); 

그들 중 누구도 작동하지 않습니다. 내가 놓친 게 있니?

감사합니다.

+0

'socket.emit'은 서버가 아닌 브라우저에서 클라이언트로 이벤트를 보냅니다. – fent

+0

왜 서버 측 크로스 룸 이벤트가 필요합니까? node.js 용 socket.io 클라이언트가 있지만이 시나리오에서는 과도한 것처럼 보입니다. –

+0

@ user80222 : 동의하지 않습니다. 많은 시나리오 (예 : 서버에서 터널을 파내야하는 브라우저 호환성 문제, irc에서 시스템 관리자가 원하는 룸에 msg를 브로드 캐스트하거나 보안 연결을 위해 다른 룸에 비공개 메시지를 전달할 수 있음) 정말로 존재합니다. – user1045217

답변

0

나는 모든 코드를 조사하지 않았지만 일반적인 아이디어는 메시지를 보낼 때 클라이언트 쪽에서 이벤트 + 데이터를 내 보낸 다음 서버 측에서 이벤트를 일어날 때 방송. 아래의 일부 사이비 서버 측 코드 : 클라이언트 측에

io.sockets.on('connection', function (socket) { 
    console.log('Socket connection established.'); 
    socket.on('message event', function(data){ 
     socket.broadcast.emit('global chat update', data);} 
    ); 
}); 

당신이 코드의 2 개 비트를 원하는 :

1) 채팅 이벤트가 발생, 같은 :

socket.emit('message event', data) 

2) 글로벌 채팅 업데이트가있을 때 최신 채팅을 표시하는 것.

socket.on('global chat update', function(data) { // your script to update chat data here };