Skip to content
This repository
Browse code

Merge git://github.com/pietern/txwebsockets

  • Loading branch information...
commit 29747e1b3b9c81b5f80dc55c2d97820a86812272 2 parents 9d43abb + fcfa1c0
Jon Thonstad authored

Showing 2 changed files with 13 additions and 11 deletions. Show diff stats Hide diff stats

  1. +1 1  examples/interact.py
  2. +12 10 txwebsockets/__init__.py
2  examples/interact.py
@@ -24,7 +24,7 @@ def hexify(self, sto):
24 24
25 25 if __name__ == '__main__':
26 26 from twisted.internet import reactor
27   - mo = MyOperations()
  27 + mo = MyOperations
28 28
29 29 factory = WebSocketFactory(mo)
30 30
22 txwebsockets/__init__.py
@@ -48,7 +48,7 @@ def _out(self, str):
48 48 def after_connection(self):
49 49 pass
50 50
51   -class WebSocketServer(LineReceiver):
  51 +class WebSocketConnection(LineReceiver):
52 52 HDR_ORIGIN = re.compile('Origin\:\s+(.*)')
53 53 HDR_LOCATION = re.compile('GET\s+(.*)\s+HTTP\/1.1', re.I)
54 54 HDR_HOST = re.compile('Host\:\s+(.*)')
@@ -56,8 +56,9 @@ class WebSocketServer(LineReceiver):
56 56 SEC_WS_KEY1 = re.compile('Sec-WebSocket-Key1\:\s+(.*)')
57 57 SEC_WS_KEY2 = re.compile('Sec-WebSocket-Key2\:\s+(.*)')
58 58
59   - def __init__(self):
60   -
  59 + def __init__(self,oper):
  60 + self.oper = oper
  61 +
61 62 self.old_hdr = '''HTTP/1.1 101 Web Socket Protocol Handshake\r
62 63 Upgrade: WebSocket\r
63 64 Connection: Upgrade\r
@@ -74,10 +75,10 @@ def __init__(self):
74 75
75 76 def connectionMade(self):
76 77 self.setRawMode()
77   - self.factory.oper.on_connect()
  78 + self.oper.on_connect()
78 79
79 80 def lineReceived(self, line):
80   - self.factory.oper.on_read(line)
  81 + self.oper.on_read(line)
81 82
82 83 def rawDataReceived(self, line):
83 84 origin, location, host, token = self._parseHeaders(line)
@@ -88,11 +89,11 @@ def rawDataReceived(self, line):
88 89 self.sendLine(self.hdr % (origin, host, location, token))
89 90 self.delimiter='\xff'
90 91 self.setLineMode()
91   - self.factory.oper.setWriteHandler(self.sendLine)
92   - self.factory.oper.after_connection()
  92 + self.oper.setWriteHandler(self.sendLine)
  93 + self.oper.after_connection()
93 94
94 95 def connectionLost(self, reason):
95   - self.factory.oper.on_close(reason)
  96 + self.oper.on_close(reason)
96 97
97 98 def _parseHeaders(self, buf):
98 99 if buf == None:
@@ -139,9 +140,10 @@ def _filterella(self, w):
139 140 return x
140 141
141 142 class WebSocketFactory(Factory):
142   - protocol = WebSocketServer
  143 + protocol = WebSocketConnection
143 144
144 145 def __init__(self, oper=BasicOperations):
145 146 self.oper=oper
146 147
147   -
  148 + def buildProtocol(self, addr):
  149 + return self.protocol(self.oper())

0 comments on commit 29747e1

Please sign in to comment.
Something went wrong with that request. Please try again.