import os, sys, time, json, random

import tornado.ioloop as ioloop
import tornado.web as web
import tornado.websocket as websocket
from tornado.escape import json_encode

import logging
logging.basicConfig(level=logging.DEBUG)

file_path = os.path.dirname(os.path.realpath(__file__))
static_path = os.path.join(file_path, 'static')

data_till_now = []
class AcqWebSocket(websocket.WebSocketHandler):
    listeners = set([])
    def open(self):
        logging.log(logging.DEBUG, '<%s> connected' % repr(self))
        AcqWebSocket.listeners.add(self)
        self.write_message(json_encode(data_till_now))
    def on_message(self, message):
        logging.log(logging.INFO, '<%s> received: %s' % (repr(self), repr(message)))
    def on_close(self):
        logging.log(logging.DEBUG, '<%s> disconnected' % repr(self))
        AcqWebSocket.listeners.discard(self)
    @staticmethod
    def write_all(message, **kwargs):
        for listener in AcqWebSocket.listeners:
            listener.write_message(message, **kwargs)

class ControlHandler(web.RequestHandler):
    @web.removeslash
    def put(self, command):
        logging.log(logging.INFO, '<%s> command: %s' % (repr(self), command))

t0 = 0
def send_data_test():
    global t0, data_till_now
    ts = time.time() - t0
    if ts > 300:
        t0, ts = time.time(), 0
        data_till_now = []
        AcqWebSocket.write_all(json_encode({"kind": "clear"}))
    datum = [random.randint(0,(1<<10)-1),ts]
    AcqWebSocket.write_all(json_encode([datum]))
    data_till_now.append(datum)

def send_stim_test():
    AcqWebSocket.write_all(json_encode({
        "kind": "stim",
        "data": [(1 << random.randint(0,7)) for i in xrange(2048)]
    }))

application = web.Application([
    (r"/()$", web.StaticFileHandler, {'path': os.path.join(static_path, 'index.html')}),
    (r"/socket", AcqWebSocket),
    (r"/control/(.+)", ControlHandler),
    (r"/(.+)", web.StaticFileHandler, {'path': static_path})
])

if __name__ == '__main__':
    application.listen(8888)
    ioloop.PeriodicCallback(send_data_test, 100).start()
    ioloop.PeriodicCallback(send_stim_test, 4000).start()
    ioloop.IOLoop.instance().start()

@@ -163,16 +163,29 @@
 n_encode
+, json_decode
 %0A%0Aimport
@@ -1121,16 +1121,100 @@
 mmand):%0A
+        body = self.request.body%0A        body = json_decode(body) if body else None%0A
         
@@ -1252,23 +1252,48 @@
  command
+ %3C%25s%3E
 : %25s' %25
+%0A                   
  (repr(s
@@ -1305,16 +1305,28 @@
  command
+, repr(body)
 ))%0A%0At0 =

Test receiving json command data