Guest User

Untitled

a guest
Apr 22nd, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1.  
  2. import tornado.websocket
  3. import tornado.ioloop
  4.  
  5. from time import sleep
  6.  
  7. line = 0 #the current message to be send
  8. listeners = [] #for holding the connecting
  9.  
  10. class WebSocketHandler(tornado.websocket.WebSocketHandler):
  11.     def open(self):
  12.         listeners.append(self)#add to list of connections
  13.     def on_close(self):
  14.         listeners.remove(self)#remove from list of connections
  15.    
  16.  
  17. application = tornado.web.Application([
  18.     (r"/websocket", WebSocketHandler),
  19. ])
  20.  
  21. def main():
  22.     global line
  23.     #line=ser.readline() #get the message from the serial
  24.     print(listeners) # <-- for debugging connections
  25.     line = line + 1
  26.     sleep(0.01)
  27.     for i in range(0, len(listeners)): #go trough all connections and send the new message
  28.         listeners[i].write_message(str(line))  
  29.  
  30. if __name__ == "__main__":
  31.     application.listen(8888)
  32.     while(1):
  33.         tornado.ioloop.IOLoop.instance().add_callback(main)
Advertisement
Add Comment
Please, Sign In to add comment