Skip to content

Commit

Permalink
Added firing of disconnect event for WebSocketsDispatcher. Fixes Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
prologic committed May 14, 2014
1 parent 3303197 commit 73fdad5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,8 @@ Change Log
==========


- :feature`103` Added the firing of a ``disconnect`` event for the
WebSocketsDispatcher.
- :bug:`102` Fixed minor bug with WebSocketsDispatcher causing superflusous
``connect()`` events from being fired.
- :bug:`100` Fixed returned Content-Type in JSON-RPC Dispatcher.
Expand Down
5 changes: 4 additions & 1 deletion circuits/web/websockets/dispatcher.py
Expand Up @@ -2,13 +2,15 @@
# Date: 26th February 2011
# Author: James Mills, prologic at shortcircuit dot net dot au


import base64
import hashlib


from circuits.six import b
from circuits.net.events import connect
from circuits.web.errors import httperror
from circuits import handler, BaseComponent
from circuits.net.events import connect, disconnect
from circuits.protocols.websocket import WebSocketCodec


Expand Down Expand Up @@ -113,4 +115,5 @@ def _on_response_complete(self, e, value):
@handler("disconnect")
def _on_disconnect(self, sock):
if sock in self._codecs:
self.fire(disconnect(sock), self._wschannel)
del self._codecs[sock]
32 changes: 29 additions & 3 deletions tests/web/test_websockets.py
Expand Up @@ -4,20 +4,30 @@


from circuits import Component
from circuits.net.sockets import write
from circuits.web.servers import Server
from circuits.web.controllers import Controller
from circuits.net.sockets import close, write
from circuits.web.websockets import WebSocketClient, WebSocketsDispatcher


from .helpers import urlopen


class Echo(Component):

channel = "wsserver"

def init(self):
self.clients = []

def connect(self, sock, host, port):
self.clients.append(sock)
print("WebSocket Client Connected:", host, port)
self.fire(write(sock, "Welcome {0:s}:{1:d}".format(host, port)))

def disconnect(self, sock):
self.clients.remove(sock)

def read(self, sock, data):
self.fire(write(sock, "Received: " + data))

Expand All @@ -39,14 +49,18 @@ def read(self, data):
self.response = data


def test(manager, watcher):
def test(manager, watcher, webapp):
server = Server(("localhost", 8123)).register(manager)
watcher.wait("ready")

Echo().register(server)
echo = Echo().register(server)
Root().register(server)
watcher.wait("registered", channel="wsserver")

f = urlopen(webapp.server.http.base)
s = f.read()
assert s == b"Hello World!"

watcher.clear()

WebSocketsDispatcher("/websocket").register(server)
Expand All @@ -56,6 +70,8 @@ def test(manager, watcher):
client = Client().register(manager)
watcher.wait("connected", channel="wsclient")

assert len(echo.clients) == 1

watcher.wait("read", channel="ws")
assert client.response.startswith("Welcome")
watcher.clear()
Expand All @@ -64,6 +80,16 @@ def test(manager, watcher):
watcher.wait("read", channel="ws")
assert client.response == "Received: Hello!"

f = urlopen(webapp.server.http.base)
s = f.read()
assert s == b"Hello World!"

assert len(echo.clients) == 1

client.fire(close(), "ws")
watcher.wait("disconnect", channel="wsserver")
assert len(echo.clients) == 0

client.unregister()
watcher.wait("unregistered")
watcher.clear()
Expand Down

0 comments on commit 73fdad5

Please sign in to comment.