-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py.1
More file actions
86 lines (69 loc) · 2 KB
/
Copy pathserver.py.1
File metadata and controls
86 lines (69 loc) · 2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import threading
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
from random import randint
global running
clients = []
c=0
#Initialize TOrnado to use 'GET' and load index.html
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate())
#Code for handling the data sent from the webpage
class WSHandler(tornado.websocket.WebSocketHandler):
active=False
count=0
def check_origin(self, origin):
return True
def open(self):
clients.append(self)
print 'connection opened...'
print clients
def on_message(self, message):
if clients.index(self) == 0:
print 'received:', message
def on_close(self):
clients.remove(self)
print 'connection closed...'
print clients
def msg(self):
self.write_message(u"Message: " + str(self.count)+ " " + str(clients))
def activate(self):
if self.active == False:
self.active = True
self.write_message(u"controlling" + str(randint(3333,4444)))
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "./resources"}),
])
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Ready"
while running:
time.sleep(1) # sleep for 200 ms
if len(clients) != 0:
clients[0].count = clients[0].count + 1
clients[0].msg
clients[0].activate()
if clients[0].count == 11:
clients[0].close()
clients.remove(clients[0]);
for c in clients:
c.msg()
if __name__ == "__main__":
running = True
thread1 = myThread(1, "Thread-1", 1)
thread1.setDaemon(True)
thread1.start()
application.listen(9093) #9093 #starts the websockets connection
tornado.ioloop.IOLoop.instance().start()