Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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.

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

Showing 3 unique commits by 1 author.

Sep 10, 2011
Iuri Diniz new class WebSocketFactory
Factory which wraps another factory to provide WebSockets transports for
all of its protocols.
3cf7b7c
Iuri Diniz Example of wrapping a simple Echo Protocol 9b32bcf
Iuri Diniz undo comment flash policy server 05b031f
This page is out of date. Refresh to see the latest.
47  echo.html
... ...
@@ -0,0 +1,47 @@
  1
+<html>
  2
+  <head>
  3
+    <title>WebSocket example: echo service</title>
  4
+  </head>
  5
+  <body>
  6
+    <h1>WebSocket example: echo service</h1>
  7
+    <script type="text/javascript">
  8
+        var ws = new WebSocket("ws://localhost:8080/echo");
  9
+        ws.onmessage = function(evt) {
  10
+            var data = evt.data;
  11
+            var target = document.getElementById("received");
  12
+            target.value = target.value + data;
  13
+        };
  14
+
  15
+        ws.onopen = function(evt) {
  16
+            var target = document.getElementById('conn_status');
  17
+            target.value = "Connected";
  18
+        };
  19
+
  20
+        ws.onerror = function(evt) {
  21
+            target = document.getElementById('conn_status');
  22
+            target.value = 'Error';
  23
+        };
  24
+        ws.onclose = function(evt) {
  25
+            target = document.getElementById('conn_status');
  26
+            target.value = 'Closed';
  27
+        };
  28
+
  29
+        window.send_data = function() {
  30
+            ws.send(document.getElementById("send_input").value);
  31
+        };
  32
+    </script>
  33
+    <form>
  34
+      <label for="send_input">Text to send</label>
  35
+      <input type="text" name="send_input" id="send_input"/>
  36
+      <input type="submit" name="send_submit" id="send_submit" value="Send"
  37
+             onclick="send_data(); return false"/>
  38
+      <br/>
  39
+      <label for="received">Received text</label>
  40
+      <textarea name="received" id="received"></textarea>
  41
+      <br/>
  42
+      <label for="status">Status</label>
  43
+      <input type="text" name="status" id="conn_status" value="Not Connected"/>
  44
+
  45
+    </form>
  46
+  </body>
  47
+</html>
14  simple_server.py
@@ -16,8 +16,15 @@
16 16
 from twisted.web.static import File
17 17
 from twisted.internet import task
18 18
 
19  
-from websocket import WebSocketHandler, WebSocketSite
  19
+from websocket import WebSocketHandler, WebSocketSite, WebSocketFactory
20 20
 
  21
+class Echo(Protocol):
  22
+    def dataReceived(self, data):
  23
+        print "Received data '%s'" % data.strip()
  24
+        self.transport.write(data)
  25
+
  26
+class EchoFactory(Factory):
  27
+    protocol = Echo
21 28
 
22 29
 class Testhandler(WebSocketHandler):
23 30
     def __init__(self, transport):
@@ -71,6 +78,11 @@ def connectionMade(self):
71 78
     root = File('.')
72 79
     site = WebSocketSite(root)
73 80
     site.addHandler('/test', Testhandler)
  81
+
  82
+    # example of wrapping a factory in order to provide WebSockets transport
  83
+    echo_factory_wrapped = WebSocketFactory(EchoFactory())
  84
+    site.addHandler('/echo', echo_factory_wrapped.buildHandler)
  85
+
74 86
     reactor.listenTCP(8080, site)
75 87
     # run policy file server
76 88
     factory = Factory()
37  websocket.py
@@ -18,6 +18,7 @@
18 18
 import struct
19 19
 
20 20
 from twisted.internet import interfaces
  21
+from twisted.protocols.policies import WrappingFactory, ProtocolWrapper
21 22
 from twisted.web.http import datetimeToString
22 23
 from twisted.web.http import _IdentityTransferDecoder
23 24
 from twisted.web.server import Request, Site, version, unquote
@@ -483,7 +484,41 @@ def dataReceived(self, data):
483 484
                     self._data.append(data)
484 485
                 break
485 486
 
  487
+class WebSocketFactory(WrappingFactory):
  488
+    """
  489
+    Factory which wraps another factory to provide WebSockets transports for
  490
+    all of its protocols.
  491
+    """
  492
+
  493
+    protocol = ProtocolWrapper
  494
+
  495
+    def buildHandler(self, transport):
  496
+        """"
  497
+        Called in order to build C{WebSocketHandler} objects.
  498
+    
  499
+        @ivar transport: a C{WebSocketTransport} instance.
  500
+        @type: L{WebSocketTransport}
  501
+        """
  502
+        
  503
+        protocol = self.buildProtocol(transport.getHost())
  504
+
  505
+        handler = WebSocketHandler(transport)
  506
+
  507
+        def connectionMade():
  508
+            protocol.makeConnection(transport)
  509
+            
  510
+        def frameReceived(frame):
  511
+            protocol.dataReceived(frame)
  512
+
  513
+        def connectionLost(reason):
  514
+            protocol.connectionLost(reason)
  515
+        
  516
+        handler.connectionMade = connectionMade
  517
+        handler.frameReceived = frameReceived
  518
+        handler.connectionLost = connectionLost
  519
+        
  520
+        return handler
486 521
 
487 522
 
488  
-__all__ = ["WebSocketHandler", "WebSocketSite"]
  523
+__all__ = ["WebSocketHandler", "WebSocketSite", "WebSocketFactory"]
489 524
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.