-
Notifications
You must be signed in to change notification settings - Fork 2
/
websockets.html
207 lines (193 loc) · 6.86 KB
/
websockets.html
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!-- Copyright (c) 2016 Neeraj Sharma neeraj.sharma@alumni.iitg.ernet.in
All rights reserved.
See LICENSE for license.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>SignalBlocks Streaming WebSocketsDemo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="smoothie.js"></script>
<style type="text/css">
body {
background-color: black;
color: darkgray;
}
table {
margin: 0;
padding: 0;
border: 0;
border-width: 0;
border-spacing: 0;
border-collapse: collapse;
vertical-align: baseline;
align: center;
}
tr {
margin: 0;
padding: 0;
border: 0;
border-width: 0;
border-spacing: 0;
}
td {
margin: 0;
padding: 0;
border: 0;
border-width: 0;
border-spacing: 0;
}
canvas#mycanvas {
margin: 0px;
padding: 0px;
display: block;
width: 100%;
height: 100%;
}
td img#top-middle {
width:100%; height:93px;
}
td img#bottom-middle {
width:100%; height:45px;
}
td img#left {
width:100%; height:100%;
}
td img#right {
width:100%; height:100%;
}
td img#right-panel {
width: 100%; height: 100%;
}
img {
display: block;
}
</style>
<script type="text/javascript">
window.onload = function() {
var log = document.getElementById("log");
var time_status = document.getElementById("time_status");
var tsample_element = document.getElementById("tsample");
var timeSeries = new TimeSeries();
var smoothieChart = new SmoothieChart({
millisPerPixel: 41,
timestampFormatter:SmoothieChart.timeFormatter, // show timestamps
grid: {
borderVisible:false, // no border
strokeStyle: 'rgb(125, 0, 0)',
fillStyle: 'rgb(60, 0, 0)',
lineWidth: 1,
millisPerLine: 1000,
verticalSections: 4
}
});
smoothieChart.addTimeSeries(timeSeries, {
strokeStyle:'rgb(0, 255, 0)',
//fillStyle:'rgba(0, 255, 0, 0.4)',
lineWidth:3
});
smoothieChart.streamTo(document.getElementById("mycanvas"), 1000);
//var img_top_left = document.getElementById("top-left");
//smoothieChart.canvas.drawImage(img,10,10, 150, 180);
if (window["WebSocket"]) {
var load_time_millisec = 0;
var load_x = -1;
var millisec_between_samples = 200; // read from websocket (TODO)
tsample_element.innerHTML = millisec_between_samples;
// when the time difference between last sample received and current
// time is more than the threshold then reset again to avoid lagging
// display.
var time_adjustment_threshold_millisec = 1000;
var conn = new WebSocket("ws://localhost:8585/1");
// var conn = new WebSocket("ws://"+window.location.host+"/ws");
conn.onmessage = function(evt) {
var json = jQuery.parseJSON(evt.data);
var current_time_millisec = new Date().getTime();
if (load_x < 0) { // init for the first time (after connection)
// TODO read millisec_between_samples as well from
// the websocket server. Maybe do this as a special packet
// which is the first one.
// TODO Fix the current assumption that at least one element in
// json array and
// also json[0].data array, but this need not be the case.
var last_index = json[0].data.length - 1;
load_x = json[0].data[last_index].x;
load_time_millisec = current_time_millisec;
console.log("load_x " + load_x);
time_status.innerHTML = "<b>Init</b>";
} else {
var last_index = json[0].data.length - 1;
// TODO worry about wraparound of value of x
var delta_millisec = current_time_millisec -
(load_time_millisec +
((json[0].data[last_index].x - load_x) * millisec_between_samples));
if (delta_millisec > 0)
{
time_status.innerHTML = "<font color=\"cyan\">" + delta_millisec +
"</font> msec behind";
} else if (delta_millisec < 0)
{
time_status.innerHTML = "<font color=\"green\">" + Math.abs(delta_millisec) +
"</font> msec ahead";
} else {
time_status.innerHTML = "Synced";
}
if (delta_millisec > time_adjustment_threshold_millisec)
{
// reinit because the time lag is too much
var last_index = json[0].data.length - 1;
load_x = json[0].data[last_index].x;
load_time_millisec = current_time_millisec;
console.log("load_x " + load_x);
time_status.innerHTML = "<b>ReInit</b>";
}
}
log.innerHTML = json[0].name + " = [" +
"{x = " + json[0].data[0].x + ", " +
"y = " + json[0].data[0].y + "}, ...] (size=" +
json[0].data.length + ")";
for (i = 0; i < json[0].data.length; i++) {
// Date().getTime() gives the number of milliseconds since
// 1970/01/01
// TODO worry about wraparound of value of x
timeSeries.append(load_time_millisec + ((json[0].data[i].x - load_x) * millisec_between_samples), parseFloat(json[0].data[i].y));
}
};
conn.onclose = function(evt) {
log.innerHTML = "Connection closed";
};
} else {
log.innerHTML = "Browser does not support WebSockets";
}
};
</script>
</head>
<body>
<ul>
<li>Tsample = <span id="tsample"></span> (milliseconds between samples).</li>
<li>The signal is <span id="time_status"></span> your clock.</li>
<li><div id="log"></div></li>
</ul>
<table cellspacing="0" cellpadding="0" rowspacing="0" rowpadding="0">
<tr>
<td><img id="top-left" src="oscilloscope_top_left_border.png"/></td>
<td><img id="top-middle" src="oscilloscope_top_middle.png"/></td>
<td><img id="top-right" src="oscilloscope_top_right_border.png"/></td>
<td rowspan="3" ><img id="right-panel" src="oscilloscope_right_panel_sized.png"/></td>
</tr>
<tr>
<td><img id="left" src="oscilloscope_left_border.png"/></td>
<td id="canvas"> <canvas id="mycanvas" width="480" height="240"></canvas></td>
<td><img id="right" src="oscilloscope_right_border.png"/></td>
</tr>
<tr>
<td><img id="bottom-left" src="oscilloscope_bottom_left_border.png"/></td>
<td><img id="bottom-middle" src="oscilloscope_bottom.png"/></td>
<td><img id="bottom-right" src="oscilloscope_bottom_right_border.png"/></td>
</tr>
</table>
<br/>
<small>Thaks to Stelios and Petros for permission to use the oscilloscope image adapted from their <a style="color:green" href="http://www.evilwindowdog.com/soundbeam/">SoundBeam iPhone App</a>.<br/>
</body>
</html>