Skip to content

Commit

Permalink
added server disconnect support
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 6, 2015
1 parent cbb1a82 commit 5633917
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
8 changes: 7 additions & 1 deletion example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ def send_room_message(sid, message):
room=message['room'], namespace='/test')


@socketio.on('disconnect request', namespace='/test')
def disconnect_request(sid):
socketio.disconnect(sid, namespace='/test')


@socketio.on('connect', namespace='/test')
def test_connect(sid, environ):
socketio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid)
socketio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test')


@socketio.on('disconnect', namespace='/test')
Expand Down
9 changes: 6 additions & 3 deletions example/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

socket.on('my response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});
socket.on('connect', function() {
socket.emit('my event', {data: 'I\'m connected!'});
});
socket.on('my response', function(msg) {
$('#log').append('<br>Received: ' + msg.data);
});

// event handler for server sent data
// the data is displayed in the "Received" section of the page
Expand Down Expand Up @@ -79,6 +79,9 @@ <h2>Send:</h2>
<input type="text" name="close_room" id="close_room" placeholder="Room Name">
<input type="submit" value="Close Room">
</form>
<form id="disconnect" method="POST" action="#">
<input type="submit" value="Disconnect">
</form>
<h2>Receive:</h2>
<div><p id="log"></p></div>
</body>
Expand Down
6 changes: 3 additions & 3 deletions socketio/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def get_participants(self, namespace, room):
self._clean_rooms()

def connect(self, sid, namespace):
"""Record a client connection to a namespace."""
"""Register a client connection to a namespace."""
self.enter_room(sid, namespace, None)
self.enter_room(sid, namespace, sid)

def disconnect(self, sid, namespace):
"""Record a client disconnect event."""
"""Register a client disconnect event."""
if namespace == '/':
namespace_list = list(six.iterkeys(self.rooms))
namespace_list = list(self.get_namespaces())
else:
namespace_list = [namespace]
for n in namespace_list:
Expand Down
17 changes: 17 additions & 0 deletions socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ def rooms(self, sid, namespace=None):
namespace = namespace or '/'
return self.manager.get_rooms(sid, namespace)

def disconnect(self, sid, namespace=None):
"""Disconnect a client.
:param sid: Session ID of the client.
:param namespace: The Socket.IO namespace to disconnect. If this
argument is omitted the default namespace is used.
"""
self.logger.info('Disconnecting %s]', sid)
if namespace is None or namespace == '/':
for namespace in self.manager.get_namespaces():
self._send_packet(sid, packet.Packet(packet.DISCONNECT,
namespace=namespace))
else:
self._send_packet(sid, packet.Packet(packet.DISCONNECT,
namespace=namespace))
self.manager.disconnect(sid, namespace=namespace)

def handle_request(self, environ, start_response):
"""Handle an HTTP request from the client.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ def test_invalid_callback(self, eio):
self.assertRaises(ValueError, s._handle_eio_message, '123',
'32["foo",2]')

def test_disconnect_all(self, eio):
s = server.Server()
s._handle_eio_connect('123', 'environ')
s._handle_eio_message('123', '0/foo')
s.disconnect('123')
s.eio.send.assert_any_call('123', '1/foo', binary=False)
s.eio.send.assert_any_call('123', '1', binary=False)

def test_disconnect_namespace(self, eio):
s = server.Server()
s._handle_eio_connect('123', 'environ')
s._handle_eio_message('123', '0/foo')
s.disconnect('123', namespace='/foo')
s.eio.send.assert_any_call('123', '1/foo', binary=False)

def test_logger(self, eio):
s = server.Server(logger=False)
self.assertEqual(s.logger.getEffectiveLevel(), logging.ERROR)
Expand Down

0 comments on commit 5633917

Please sign in to comment.