我创建了一些按钮来连接和断开我的命名空间:
<div id="message">no message</div>
<button id="disNamespace1" type="button"> Disconnect from namespace1</button>
<button id="disNamespace2" type="button"> Disconnect from namespace2</button>
<button id="conNamespace1" type="button"> connection to namespace1</button>
<button id="conNamespace2" type="button"> connection to namespace2</button>
我正在像这样处理这个按钮:
$('#conNamespace1').click(function(ev){
mySocket = io.connect('http://localhost:1337/namespace1');
mySocket.on("msg:from:namspace1", function(msg){
$('#message').text(msg);
});
});
$('#conNamespace2').click(function(ev){
mySocket = io.connect('http://localhost:1337/namespace2');
mySocket.on("msg:from:namspace2", function(msg){
$('#message').text(msg);
});
});
$('#disNamespace1').click(function(ev){
console.log('trying disconnect');
io.sockets['http://localhost:1337'].disconnect();
});
$('#disNamespace2').click(function(ev){
io.sockets['http://localhost:1337'].disconnect();
});
我的服务器:
socketNamespace1 = io.of('/namespace1');
socketNamespace2 = io.of('/namespace2');
socketNamespace1.on('connection', function(mySocket){
console.log('namespace1: new user ');
mySocket.emit('msg:from:namspace1', 'msg:from:namspace1');
mySocket.on('disconnect', function(){
console.log('disconnecting from namespace1');
});
});
socketNamespace2.on('connection', function(mySocket){
console.log('namespace2: new user ');
mySocket.emit('msg:from:namspace2', 'msg:from:namspace2');
mySocket.on('disconnect', function(){
console.log('disconnecting from namespace2');
});
});
我的第一次连接和断开连接都很好,但是当第二次单击连接按钮时,它不再起作用了。问题是什么 ?