Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

new class WebSocketFactory (WebSocket Wrapper) #16

Open
wants to merge 3 commits into from

1 participant

Iuri Diniz
Iuri Diniz

The websocket implementation as a standalone wrapper for Twisted protocols (available at http://paste.pocoo.org/show/451569/ ) allows us to make a twisted protocols available over websocket

I wrote a new class in order to provide this behaviour too and I submitted in theses commits

3cf7b7c → class implementation

9b32bcf → example of use

I don't know how to write tests for it, so, sorry, there are no tests for this newer class.

iuridiniz added some commits
Iuri Diniz iuridiniz new class WebSocketFactory
Factory which wraps another factory to provide WebSockets transports for
all of its protocols.
3cf7b7c
Iuri Diniz iuridiniz Example of wrapping a simple Echo Protocol 9b32bcf
Iuri Diniz iuridiniz undo comment flash policy server 05b031f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 11, 2011
  1. Iuri Diniz

    new class WebSocketFactory

    iuridiniz authored
    Factory which wraps another factory to provide WebSockets transports for
    all of its protocols.
  2. Iuri Diniz
  3. Iuri Diniz
This page is out of date. Refresh to see the latest.
Showing with 96 additions and 2 deletions.
  1. +47 −0 echo.html
  2. +13 −1 simple_server.py
  3. +36 −1 websocket.py
47 echo.html
View
@@ -0,0 +1,47 @@
+<html>
+ <head>
+ <title>WebSocket example: echo service</title>
+ </head>
+ <body>
+ <h1>WebSocket example: echo service</h1>
+ <script type="text/javascript">
+ var ws = new WebSocket("ws://localhost:8080/echo");
+ ws.onmessage = function(evt) {
+ var data = evt.data;
+ var target = document.getElementById("received");
+ target.value = target.value + data;
+ };
+
+ ws.onopen = function(evt) {
+ var target = document.getElementById('conn_status');
+ target.value = "Connected";
+ };
+
+ ws.onerror = function(evt) {
+ target = document.getElementById('conn_status');
+ target.value = 'Error';
+ };
+ ws.onclose = function(evt) {
+ target = document.getElementById('conn_status');
+ target.value = 'Closed';
+ };
+
+ window.send_data = function() {
+ ws.send(document.getElementById("send_input").value);
+ };
+ </script>
+ <form>
+ <label for="send_input">Text to send</label>
+ <input type="text" name="send_input" id="send_input"/>
+ <input type="submit" name="send_submit" id="send_submit" value="Send"
+ onclick="send_data(); return false"/>
+ <br/>
+ <label for="received">Received text</label>
+ <textarea name="received" id="received"></textarea>
+ <br/>
+ <label for="status">Status</label>
+ <input type="text" name="status" id="conn_status" value="Not Connected"/>
+
+ </form>
+ </body>
+</html>
14 simple_server.py
View
@@ -16,8 +16,15 @@
from twisted.web.static import File
from twisted.internet import task
-from websocket import WebSocketHandler, WebSocketSite
+from websocket import WebSocketHandler, WebSocketSite, WebSocketFactory
+class Echo(Protocol):
+ def dataReceived(self, data):
+ print "Received data '%s'" % data.strip()
+ self.transport.write(data)
+
+class EchoFactory(Factory):
+ protocol = Echo
class Testhandler(WebSocketHandler):
def __init__(self, transport):
@@ -71,6 +78,11 @@ def connectionMade(self):
root = File('.')
site = WebSocketSite(root)
site.addHandler('/test', Testhandler)
+
+ # example of wrapping a factory in order to provide WebSockets transport
+ echo_factory_wrapped = WebSocketFactory(EchoFactory())
+ site.addHandler('/echo', echo_factory_wrapped.buildHandler)
+
reactor.listenTCP(8080, site)
# run policy file server
factory = Factory()
37 websocket.py
View
@@ -18,6 +18,7 @@
import struct
from twisted.internet import interfaces
+from twisted.protocols.policies import WrappingFactory, ProtocolWrapper
from twisted.web.http import datetimeToString
from twisted.web.http import _IdentityTransferDecoder
from twisted.web.server import Request, Site, version, unquote
@@ -483,7 +484,41 @@ def dataReceived(self, data):
self._data.append(data)
break
+class WebSocketFactory(WrappingFactory):
+ """
+ Factory which wraps another factory to provide WebSockets transports for
+ all of its protocols.
+ """
+
+ protocol = ProtocolWrapper
+
+ def buildHandler(self, transport):
+ """"
+ Called in order to build C{WebSocketHandler} objects.
+
+ @ivar transport: a C{WebSocketTransport} instance.
+ @type: L{WebSocketTransport}
+ """
+
+ protocol = self.buildProtocol(transport.getHost())
+
+ handler = WebSocketHandler(transport)
+
+ def connectionMade():
+ protocol.makeConnection(transport)
+
+ def frameReceived(frame):
+ protocol.dataReceived(frame)
+
+ def connectionLost(reason):
+ protocol.connectionLost(reason)
+
+ handler.connectionMade = connectionMade
+ handler.frameReceived = frameReceived
+ handler.connectionLost = connectionLost
+
+ return handler
-__all__ = ["WebSocketHandler", "WebSocketSite"]
+__all__ = ["WebSocketHandler", "WebSocketSite", "WebSocketFactory"]
Something went wrong with that request. Please try again.