-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsend.py
executable file
·40 lines (32 loc) · 1.19 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
from tornado.ioloop import PeriodicCallback
import tornado.web
from random import randint #Random generator
#Config
port = 9000 #Websocket Port
timeInterval= 2000 #Milliseconds
class WSHandler(tornado.websocket.WebSocketHandler):
#check_origin fixes an error 403 with Tornado
#http://stackoverflow.com/questions/24851207/tornado-403-get-warning-when-opening-websocket
def check_origin(self, origin):
return True
def open(self):
#Send message periodic via socket upon a time interval
self.callback = PeriodicCallback(self.send_values, timeInterval)
self.callback.start()
def send_values(self):
#Generates random values to send via websocket
self.write_message(str(randint(1,10)) + ';' + str(randint(1,10)) + ';' + str(randint(1,10)) + ';' + str(randint(1,10)))
def on_message(self, message):
pass
def on_close(self):
self.callback.stop()
application = tornado.web.Application([
(r'/', WSHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()