diff --git a/include/websock.js b/include/websock.js index af6a4e2..fc9fcaa 100644 --- a/include/websock.js +++ b/include/websock.js @@ -252,23 +252,23 @@ function init() { function open(uri) { init(); - websocket = new WebSocket(uri, 'websockify'); + websocket = new WebSocket(uri, 'base64'); websocket.onmessage = recv_message; - websocket.onopen = function(e) { + websocket.onopen = function() { Util.Debug(">> WebSock.onopen"); eventHandlers.open(); Util.Debug("<< WebSock.onopen"); }; websocket.onclose = function(e) { Util.Debug(">> WebSock.onclose"); - eventHandlers.close(); + eventHandlers.close(e); Util.Debug("<< WebSock.onclose"); }; websocket.onerror = function(e) { - Util.Debug("<< WebSock.onerror: " + e); + Util.Debug(">> WebSock.onerror: " + e); eventHandlers.error(e); - Util.Debug("<< WebSock.onerror: "); + Util.Debug("<< WebSock.onerror"); }; } diff --git a/tests/echo.html b/tests/echo.html index 9e3c6d6..795a5f6 100644 --- a/tests/echo.html +++ b/tests/echo.html @@ -92,7 +92,7 @@ } uri = scheme + host + ":" + port; message("connecting to " + uri); - ws = new WebSocket(uri); + ws = new WebSocket(uri, "base64"); ws.onmessage = function(e) { //console.log(">> WebSockets.onmessage"); diff --git a/tests/echo.py b/tests/echo.py index c5c5243..878c31a 100755 --- a/tests/echo.py +++ b/tests/echo.py @@ -8,84 +8,68 @@ Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3) You can make a cert/key with openssl using: openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem as taken from http://docs.python.org/dev/library/ssl.html#certificates - ''' -import os, sys, socket, select +import os, sys, select, optparse sys.path.insert(0,os.path.dirname(__file__) + "/../") from websocket import WebSocketServer class WebSocketEcho(WebSocketServer): """ - WebSockets server that echo back whatever is received from the - client. All traffic to/from the client is base64 - encoded/decoded. - """ + WebSockets server that echos back whatever is received from the + client. """ buffer_size = 8096 - def new_client(self, client): + def new_client(self): """ Echo back whatever is received. """ cqueue = [] + c_pend = 0 cpartial = "" - rlist = [client] + rlist = [self.client] while True: wlist = [] - if cqueue: wlist.append(client) + if cqueue or c_pend: wlist.append(self.client) ins, outs, excepts = select.select(rlist, wlist, [], 1) if excepts: raise Exception("Socket exception") - if client in outs: + if self.client in outs: # Send queued target data to the client - dat = cqueue.pop(0) - sent = client.send(dat) - self.vmsg("Sent %s/%s bytes of frame: '%s'" % ( - sent, len(dat), self.decode(dat)[0])) - if sent != len(dat): - # requeue the remaining data - cqueue.insert(0, dat[sent:]) + c_pend = self.send_frames(cqueue) + cqueue = [] - - if client in ins: + if self.client in ins: # Receive client data, decode it, and send it back - buf = client.recv(self.buffer_size) - if len(buf) == 0: raise self.EClose("Client closed") + frames, closed = self.recv_frames() + cqueue.extend(frames) - if buf == '\xff\x00': - raise self.EClose("Client sent orderly close frame") - elif buf[-1] == '\xff': - if cpartial: - # Prepend saved partial and decode frame(s) - frames = self.decode(cpartial + buf) - cpartial = "" - else: - # decode frame(s) - frames = self.decode(buf) - - for frame in frames: - self.vmsg("Received frame: %s" % repr(frame)) - cqueue.append(self.encode(frame)) - else: - # Save off partial WebSockets frame - self.vmsg("Received partial frame") - cpartial = cpartial + buf + if closed: + self.send_close() + raise self.EClose(closed) if __name__ == '__main__': - try: - if len(sys.argv) < 1: raise - listen_port = int(sys.argv[1]) - except: - print "Usage: %s " % sys.argv[0] - sys.exit(1) + parser = optparse.OptionParser(usage="%prog [options] listen_port") + parser.add_option("--verbose", "-v", action="store_true", + help="verbose messages and per frame traffic") + parser.add_option("--cert", default="self.pem", + help="SSL certificate file") + parser.add_option("--key", default=None, + help="SSL key file (if separate from cert)") + parser.add_option("--ssl-only", action="store_true", + help="disallow non-encrypted connections") + (opts, args) = parser.parse_args() - server = WebSocketEcho( - listen_port=listen_port, - #verbose=True, - cert='self.pem', - web='.') + try: + if len(args) != 1: raise + opts.listen_port = int(args[0]) + except: + parser.error("Invalid arguments") + + opts.web = "." + server = WebSocketEcho(**opts.__dict__) server.start_server() diff --git a/tests/encoding.html b/tests/encoding.html deleted file mode 100644 index 846645e..0000000 --- a/tests/encoding.html +++ /dev/null @@ -1,151 +0,0 @@ - - - WebSockets Test - - - - Host:   - Port:   - Encrypt:   -   - -
- Messages:
- - - - - - - - - - - - - - diff --git a/tests/encoding.py b/tests/encoding.py deleted file mode 100755 index e4d65d1..0000000 --- a/tests/encoding.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python - -''' -WebSocket server-side load test program. Sends and receives traffic -that has a random payload (length and content) that is checksummed and -given a sequence number. Any errors are reported and counted. -''' - -import sys, os, socket, ssl, time, traceback -import random, time -from base64 import b64encode, b64decode -from codecs import utf_8_encode, utf_8_decode -from select import select - -sys.path.insert(0,os.path.dirname(__file__) + "/../") -from websocket import * - -buffer_size = 65536 -recv_cnt = send_cnt = 0 - - -def check(buf): - - if buf[0] != '\x00' or buf[-1] != '\xff': - raise Exception("Invalid WS packet") - - for decoded in decode(buf): - nums = [ord(c) for c in decoded] - print "Received nums: ", nums - - return - - -def responder(client): - cpartial = "" - socks = [client] - sent = False - received = False - - while True: - ins, outs, excepts = select(socks, socks, socks, 1) - if excepts: raise Exception("Socket exception") - - if client in ins: - buf = client.recv(buffer_size) - if len(buf) == 0: raise Exception("Client closed") - received = True - #print "Client recv: %s (%d)" % (repr(buf[1:-1]), len(buf)) - if buf[-1] == '\xff': - if cpartial: - err = check(cpartial + buf) - cpartial = "" - else: - err = check(buf) - if err: - print err - else: - print "received partitial" - cpartial = cpartial + buf - - if received and not sent and client in outs: - sent = True - #nums = "".join([unichr(c) for c in range(0,256)]) - #nums = "".join([chr(c) for c in range(1,128)]) - #nums = nums + chr(194) + chr(128) + chr(194) + chr(129) - #nums = "".join([chr(c) for c in range(0,256)]) - nums = "\x81\xff" - nums = nums + "".join([chr(c) for c in range(0,256,4)]) - nums = nums + "\x00\x40\x41\xff\x81" -# print nums - client.send(encode(nums)) -# client.send("\x00" + nums + "\xff") -# print "Sent characters 0-255" -# #print "Client send: %s (%d)" % (repr(nums), len(nums)) - -if __name__ == '__main__': - try: - if len(sys.argv) < 2: raise - listen_port = int(sys.argv[1]) - except: - print "Usage: " - sys.exit(1) - - settings['listen_port'] = listen_port - settings['daemon'] = False - settings['handler'] = responder - start_server() diff --git a/tests/latency.html b/tests/latency.html index 4112ae8..4e1b55c 100644 --- a/tests/latency.html +++ b/tests/latency.html @@ -166,7 +166,7 @@ } timestamp = (new Date()).getTime(); - arr.pushStr("^" + send_seq + ":" + timestamp + ":" + payload + "$") + arr.pushStr("^" + send_seq + ":" + timestamp + ":" + payload + "$"); send_seq ++; ws.send(arr); sent++; @@ -196,10 +196,10 @@ ws.maxBufferedAmount = 5000; ws.open(uri); - ws.on('message', function(e) { + ws.on('message', function() { recvMsg(); }); - ws.on('open', function(e) { + ws.on('open', function() { send_ref = setTimeout(sendMsg, sendDelay); }); ws.on('close', function(e) { diff --git a/tests/load.html b/tests/load.html index b1e1b32..af165dc 100644 --- a/tests/load.html +++ b/tests/load.html @@ -2,9 +2,10 @@ WebSockets Load Test - + +