현재 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') 등등 모두 같은 방식으로 작동하고 자신의 네임 스페이스 - 클라이언트의 이벤트.
도움이 되었으면 좋겠습니다. 네임 스페이스가 "기본 사용"문서에서 무시당하는 것처럼 느껴집니다.
이것은 훌륭한 답변이며 정확히 내가 알아야 할 필요가 있습니다! 고마워! – ExaMa