Space-Face commited on
Commit
3645afb
1 Parent(s): 6eb30cb

Update SpaceFace.py

Browse files
Files changed (1) hide show
  1. SpaceFace.py +14 -4
SpaceFace.py CHANGED
@@ -8,6 +8,7 @@ global conn
8
  global projectid
9
  global endpoint
10
  global users
 
11
  projectid = os.getenv("ProjectID")
12
  users = {}
13
  session = scratch3.login(os.getenv("Username"), os.getenv("Password"))
@@ -86,12 +87,16 @@ class ConnectionEndpoint:
86
  users[r] = User(message, r)
87
  responder.respond(r)
88
  responder.close()
 
 
 
89
 
90
  def thread(n):
91
  global users
92
  global conn
93
  global projectid
94
  global endpoint
 
95
  conn.set_var("channel "+str(n), "0")
96
  while True:
97
 
@@ -118,7 +123,10 @@ def thread(n):
118
  else:
119
  user = UserStartup()
120
  respond = Responder(projectid, n, acceptstaticpayload, acceptstreamingpayload, value, user)
121
- endpoint[reqendpoint].receivedMessage(payload, user, respond)
 
 
 
122
 
123
 
124
  def start_server(endpoints):
@@ -129,6 +137,8 @@ def start_server(endpoints):
129
  endpoint = endpoints
130
  conn = session.connect_cloud(projectid)
131
  threads = [threading.Thread(target=thread, args=(i+1,)) for i in range(10)]
132
- for t in threads:
133
- t.start()
134
- return t
 
 
 
8
  global projectid
9
  global endpoint
10
  global users
11
+
12
  projectid = os.getenv("ProjectID")
13
  users = {}
14
  session = scratch3.login(os.getenv("Username"), os.getenv("Password"))
 
87
  users[r] = User(message, r)
88
  responder.respond(r)
89
  responder.close()
90
+ class HeartbeatEndpoint:
91
+ def receivedMessage(self, message, user, responder):
92
+ responder.close()
93
 
94
  def thread(n):
95
  global users
96
  global conn
97
  global projectid
98
  global endpoint
99
+ heartbeater = HeartbeatEndpoint()
100
  conn.set_var("channel "+str(n), "0")
101
  while True:
102
 
 
123
  else:
124
  user = UserStartup()
125
  respond = Responder(projectid, n, acceptstaticpayload, acceptstreamingpayload, value, user)
126
+ if reqendpoint == 31:
127
+ heartbeater.receivedMessage(payload, user, respond)
128
+ else:
129
+ endpoint[reqendpoint].receivedMessage(payload, user, respond)
130
 
131
 
132
  def start_server(endpoints):
 
137
  endpoint = endpoints
138
  conn = session.connect_cloud(projectid)
139
  threads = [threading.Thread(target=thread, args=(i+1,)) for i in range(10)]
140
+ while True:
141
+ time.sleep(1)
142
+ for k, v in users:
143
+ if time.time() - v.last_request >= 300:
144
+ del users[k]