Browse Source

added server disconnect support

pull/5/head
Miguel Grinberg 10 years ago
parent
commit
5633917201
  1. 8
      example/app.py
  2. 9
      example/templates/index.html
  3. 6
      socketio/base_manager.py
  4. 17
      socketio/server.py
  5. 15
      tests/test_server.py

8
example/app.py

@ -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')

9
example/templates/index.html

@ -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
@ -79,6 +79,9 @@
<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>

6
socketio/base_manager.py

@ -27,14 +27,14 @@ class BaseManager(object):
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:

17
socketio/server.py

@ -246,6 +246,23 @@ class Server(object):
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.

15
tests/test_server.py

@ -345,6 +345,21 @@ class TestServer(unittest.TestCase):
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)

Loading…
Cancel
Save