2017-12-30 12 views
2

현재 socket.io와 간단한 채팅을하고 있습니다. 기본은 이미 작동하지만 이제는 2 개의 다른 네임 스페이스를 구현하려고합니다. 클라이언트가 buttonclick을 사용하여 한 네임 스페이스 (지원 채팅)에서 다른 네임 스페이스 (토글)로 전환 할 수있게하려고합니다.socket.io 네임 스페이스 변경

는 서버 측

//default namespace 
io.on('connection', function(socket){ 
    console.log('a user connected to the chat'); 

    socket.on('disconnect', function(){ 
     console.log('user disconnected'); 
    }); 

    socket.on('client message', function(msg){ 
     io.emit('server_message', msg); 
    }); 
}); 

//namespace /support 
var sup = io.of('/support'); 
sup.on('connection', function(socket){ 
    console.log('someone entered the support-chat'); 

    socket.on('disconnect', function(){ 
     console.log('user disconnected from support-chat'); 
    }); 

    //recieving and emitting message to all clients in namespace /support 
    socket.on('client message', function(msg){ 
     console.log('message received: ' + msg); 
     io.of('/support').emit('server_message', msg); 
    }); 
}); 

//namespace /friends 
var frnd = io.of('/friends'); 
frnd.on('connection', function(socket){ 
    console.log('someone entered the friends-chat'); 

    socket.on('disconnect', function(){ 
     console.log('user disconnected from friends-chat'); 
    }); 

    //recieving and emitting message to all clients in namespace /friends 
    socket.on('client message', function(msg){ 
     console.log('message received: ' + msg); 
     io.of('/friends').emit('server_message', msg); 
    }); 
}); 

클라이언트 측이

var socket = io.connect(); 
//toggle namespace 
      $("#support_button").click(function(){ 
       socket.disconnect(); 
       socket = io('/support'); 
       $('#messages').append($('<li>').text("You entered the Support-Chat")); 
      }); 
//toggle namespace 
      $("#friends_button").click(function(){ 
       socket.disconnect(); 
       socket = io('/friends'); 
       $('#messages').append($('<li>').text("You entered the Friends-Chat")); 
      }); 
//sending message on submit 
      $('form').submit(function(){ 
       socket.emit('client message', $('#m').val()); 
       $('#m').val(''); 
       return false; 
      }); 
//recieving message and display 
      socket.on('server_message', function(msg){ 
       $('#messages').append($('<li>').text(msg)); 
      }); 
     }); 

나는 그들이해야처럼 연결 - 및 - 이벤트가 분리 유발하기 때문에 스위치 자체가 작동하고 생각합니다. 그러나 동일한 네임 스페이스의 모든 사용자에게 이미 클라이언트로부터받은 메시지를 내보내는 데는 효과가 없습니다.

밤은이 특정 네임 스페이스?

io.of('namespace').emit(); 

내가 네임 스페이스의 사용은 오해 마십시오에서 방출에 대한 serversided 호출? 나는 지원과 친구를위한 2 개의 mainchats의 네임 스페이스 - "분할"직후에 방을 구현하고 싶었습니다. 또는 서버 측에 잘못된 네임 스페이스를 구현 했습니까? 나는 .. (..), io.of ('/ support'), on .. (..)과 io.of ('/ friends') 등등 모두 같은 방식으로 작동하고 자신의 네임 스페이스 - 클라이언트의 이벤트.

도움이 되었으면 좋겠습니다. 네임 스페이스가 "기본 사용"문서에서 무시당하는 것처럼 느껴집니다.

답변

0

기존 연결에서 네임 스페이스를 "전환"할 수 없습니다. 연결이 이루어지고 한 번 만들어지면 특정 네임 스페이스에 연결하면 변경할 수 없습니다.

현재 연결을 끊고 새 연결로 새 네임 스페이스에 연결할 수 있습니다. 그러나 네임 스페이스를 바꾸고 방을 대신 사용해야하는 경우 응용 프로그램을 사용하면 네임 스페이스 개념을 잘못 사용하게됩니다.

객실의 경우 클라이언트는 서버로 방을 전환하라는 요청을 보낼 수 있으며 서버는 기존 방에서 사용자를 제거하고 새 방에 추가 할 수 있습니다. 그런 다음 서버에서 특정 방의 모든 연결로 쉽게 브로드 캐스트 할 수 있습니다.

사실, 방은 채팅 개념을 중심으로 개발되었지만 (다른 용도가 많지만) 구현하려는 대화방에 완벽하게 적합합니다.

네임 스페이스는 객실보다 무게가 가볍습니다. 연결은 연결될 때 특정 네임 스페이스에 연결해야하며 연결 ​​중에는 변경할 수 없습니다.

반면에 객실은 훨씬 유연합니다. 서버는 주어진 연결을 언제든지 방에 연결을 추가하거나 제거 할 수 있으며 연결은 둘 이상의 방에있을 수도 있습니다.

룸과 네임 스페이스 모두 해당 컬렉션의 모든 사용자에게 브로드 캐스팅을 지원합니다.

나는 네임 스페이스가 더 많은 기능 채널과 같다고 생각합니다. 따라서 가격 변경 알림을 받으려면 "가격"변경 네임 스페이스에 연결하고 싶거나 시스템에서 발생한 일에 대한 알림을 받거나 메시지를 보내려면 "시스템"네임 스페이스에 연결하고 싶습니다. 시스템의 모든 것을 관리하십시오.

객실은 정보 공유에 관심이있는 임의의 사용자 모음이며 여러 객실에있을 수 있습니다.

+0

이것은 훌륭한 답변이며 정확히 내가 알아야 할 필요가 있습니다! 고마워! – ExaMa