Anonymous avatar Anonymous committed 2d7086a

Fix websocket.send to match expected interface

Comments (0)

Files changed (2)

geventwebsocket/websocket.py

                 return (reason, message)
 
             if opcode == self.OPCODE_PING:
-                self.send(self.OPCODE_PONG, payload)
+                self.send(payload, opcode=self.OPCODE_PONG)
                 return (self.OPCODE_PING, payload)
             elif opcode == self.OPCODE_PONG:
                 return (self.OPCODE_PONG, payload)
         return opcode < self.OPCODE_FRAG or (opcode > self.OPCODE_BINARY and 
                 opcode < self.OPCODE_CLOSE) or opcode > self.OPCODE_PONG
 
-    def send(self, opcode, message):
+    def send(self, message, opcode=OPCODE_TEXT):
         if self.websocket_closed:
             raise Exception('Connection was terminated')
 
 
     def close(self, reason, message):
         message = self._encode_text(message)
-        self.send(self.OPCODE_CLOSE, struct.pack('!H%ds' % len(message), reason, message))
+        self.send(struct.pack('!H%ds' % len(message), reason, message), opcode=self.OPCODE_CLOSE)
         self.websocket_closed = True
 
         # based on gevent/pywsgi.py

tests/test__websocket.py

         read_http(fd, code=101, reason="Switching Protocols")
 
         msg = 'Hello, websocket'
-        self.ws.send(1, msg)
+        self.ws.send(msg)
 
         preamble = fd.read(2)
         opcode, length = struct.unpack('!BB', preamble)
         read_http(fd, code=101, reason="Switching Protocols")
 
         msg = 'Hello, websocket' * 8
-        self.ws.send(1, msg)
+        self.ws.send(msg)
 
         preamble = fd.read(4)
         opcode, length_code, length = struct.unpack('!BBH', preamble)
         read_http(fd, code=101, reason="Switching Protocols")
 
         msg = 'Hello, websocket' * 4098
-        self.ws.send(1, msg)
+        self.ws.send(msg)
 
         preamble = fd.read(10)
         opcode, length_code, length = struct.unpack('!BBQ', preamble)
         read_http(fd, code=101, reason="Switching Protocols")
 
         msg = struct.pack('!BHB', 129, 23, 42)
-        self.ws.send(2, msg)
+        self.ws.send(msg, opcode=WebSocketVersion7.OPCODE_BINARY)
 
         frame = fd.read(6)
         opcode, length, first, second, third = struct.unpack('!BBBHB', frame)
 
             expected_msg = 'Reserved bits cannot be set'
 
-            bad_opcode = WebSocketVersion7.FIN | (reserved_bits << 4) | WebSocketVersion7.OPCODE_TEXT
+            bad_opcode = \
+                    WebSocketVersion7.FIN | (reserved_bits << 4) | WebSocketVersion7.OPCODE_TEXT
             fd.write(struct.pack('!BB', bad_opcode, int('10000000', 2)))
 
             frame = self.ws.wait()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.