-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsend_graph_wsh.py
58 lines (44 loc) · 1.34 KB
/
send_graph_wsh.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import json
import time
_GOODBYE_MESSAGE = u'Goodbye'
x = np.arange(0,np.pi*10,0.1).tolist()
y = np.sin(x).tolist()
data_size = len(x)
counter = 0
graph_size = 100
samples = 0
tic = time.time()
def web_socket_do_extra_handshake(request):
# This example handler accepts any request. See origin_check_wsh.py for how
# to reject access from untrusted scripts based on origin value.
pass # Always accept.
def get_graph_data():
global counter,data_size,graph_size,x,y
global samples,tic
#Calculate FPS
samples += 1
if (time.time() - tic) > 2:
print "FPS is : ",samples /(time.time() - tic)
samples = 0
tic = time.time()
counter += 1
if counter > (data_size - graph_size):
counter = 0
graph_to_send = json.dumps({
'x':x[counter:counter+graph_size],
'y':y[counter:counter+graph_size]
})
return graph_to_send
def web_socket_transfer_data(request):
while True:
line = request.ws_stream.receive_message()
if line is None:
return
if isinstance(line, unicode):
request.ws_stream.send_message(get_graph_data(), binary=False)
if line == _GOODBYE_MESSAGE:
return
else:
request.ws_stream.send_message(get_graph_data(), binary=True)
# vi:sts=4 sw=4 et