-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
258 lines (252 loc) · 10.3 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE HTML>
<html>
<head>
<title>Tweetycs - Visual Analytics for Twitter Data in Public Health</title>
<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
<!-- SVG Save -->
<!-- <script src="node_modules/d3-save-svg/build/d3-save-svg.min.js"></script> -->
<!-- DC -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.12/crossfilter.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dc/3.0.12/dc.js"></script>
<!-- Excluded data -->
<script src="http://localhost:2000/socket.io/socket.io.js"></script>
<script type="text/javascript" charset="utf-8">
var updates = 0;
$(document).ready(function () {
// Use a "/test" namespace.
// An application can open a connection on multiple namespaces, and
// Socket.IO will multiplex all those connections on a single
// physical channel. If you don't care about multiple channels, you
// can set the namespace to an empty string.
// namespace = '/test';
// Connect to the Socket.IO server.
// The connection URL has the following format:
// http[s]://<domain>:<port>[/<namespace>]
var url = 'http://localhost:2000/tweetycs'
var socket = io(url);
// Event handler for new connections.
// The callback function is invoked when a connection with the
// server is established.
socket.on('connect', function () {
socket.emit('client_event', {data: 'I\'m connected!'});
});
// Event handler for server sent data.
// The callback function is invoked whenever the server emits data
// to the client. The data is then displayed in the "Received"
// section of the page.
socket.on('server_response', function (msg) {
$('#log').append('<br>' + $('<div/>').text('Received: ' + msg.data).html());
});
socket.on('hello', function (msg) {
updates++;
if (updates === 50) {
updates = 0;
dc.redrawAll();
}
var tweet = JSON.parse(msg.tweet)
xfilter.add([tweet])
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.number + ': ' + tweet.text.substr(0, 40) + '... SENTIMENT: ' + tweet.sentiment).html());
});
// Interval function that tests message latency by sending a "ping"
// message. The server then responds with a "pong" message and the
// round trip time is measured.
var ping_pong_times = [];
var start_time;
window.setInterval(function () {
start_time = (new Date).getTime();
if (socket.connected){
socket.emit('client_ping');
console.log('ping sent')
}
else
$('#ping-pong').text('∞');
console.log(socket)
}, 1000);
// Handler for the "pong" message. When the pong is received, the
// time from the ping is stored, and the average of the last 30
// samples is average and displayed.
socket.on('server_pong', function () {
console.log('ping rcvd')
var latency = (new Date).getTime() - start_time;
ping_pong_times.push(latency);
ping_pong_times = ping_pong_times.slice(-30); // keep last 30 samples
var sum = 0;
for (var i = 0; i < ping_pong_times.length; i++)
sum += ping_pong_times[i];
$('#ping-pong').text(Math.round(10 * sum / ping_pong_times.length) / 10);
});
// Handlers for the different forms in the page.
// These accept data from the user and send it to the server in a
// variety of ways
$('form#emit').submit(function (event) {
socket.emit('client_event', {data: $('#emit_data').val()});
return false;
});
$('form#broadcast').submit(function (event) {
socket.emit('client_broadcast_event', {data: $('#broadcast_data').val()});
return false;
});
$('form#join').submit(function (event) {
socket.emit('join', {room: $('#join_room').val()});
return false;
});
$('form#leave').submit(function (event) {
socket.emit('leave', {room: $('#leave_room').val()});
return false;
});
$('form#send_room').submit(function (event) {
socket.emit('client_room_event', {room: $('#room_name').val(), data: $('#room_data').val()});
return false;
});
$('form#connect').submit(function (event) {
socket = io(url);
return false;
});
$('form#disconnect').submit(function (event) {
socket.emit('disconnect_request');
return false;
});
$('form#fetch_stream').submit(function (event) {
socket.emit('fetch_stream');
return false;
});
$('form#stop_fetch').submit(function (event) {
socket.emit('stop_fetch');
return false;
});
});
</script>
</head>
<body>
<button id="export" type="button">Save as SVG</button>
<div id="inf-sent-chart">
Users and their ....
<a class="reset" href="javascript:infSentChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>
</div>
<script async>
var svg = d3.select("#inf-sent-chart")
.append('svg');
var defs = svg.append("defs")
var infSentChart = dc.bubbleChart("#inf-sent-chart");
var data = []
var xfilter = crossfilter(data),
userDimension = xfilter.dimension(function (d) {
return d.user.screen_name;
});
// Maintain running tallies by year as filters are applied or removed
var userGroup = userDimension.group().reduce(
/* callback for when data is added to the current filter results */
function (p, v) {
p.tweetCount = p.tweetCount + 1;
p.avgSentiment = (p.avgSentiment + (+v.sentiment)) / p.tweetCount;
p.influence = +v.user.followers_count / (+v.user.friends_count + 1);
return p;
},
/* callback for when data is removed from the current filter results */
function (p, v) {
p.tweetCount = p.tweetCount - 1;
p.avgSentiment = (p.avgSentiment - (+v.sentiment)) / p.tweetCount;
p.influence = +v.user.followers_count / (+v.user.friends_count + 1);
return p;
},
/* initialize p */
function () {
return {
tweetCount: 0,
avgSentiment: 0,
influence: 0,
};
}
);
function render_plots() {
infSentChart
.width(990)
.height(500)
.margins({top: 10, right: 50, bottom: 30, left: 60})
.dimension(userDimension)
.group(userGroup)
.keyAccessor(function (p) {
return p.value.influence;
})
.valueAccessor(function (p) {
return p.value.avgSentiment;
})
.radiusValueAccessor(function (p) {
return 0.1;
})
.colorAccessor(function (p) {
return p.value.avgSentiment;
})
.x(d3.scaleLinear().domain([0, 10]))
.y(d3.scaleLinear().domain([-1, 1]))
.r(d3.scaleLinear().domain([0, 10]))
.elasticY(true)
.yAxisPadding(0.1)
.elasticX(true)
.xAxisPadding(200)
//.maxBubbleRelativeSize(0.07)
.renderHorizontalGridLines(true)
.renderVerticalGridLines(true)
.xAxisLabel('Influence')
.yAxisLabel('Sentiment Score')
.renderLabel(false)
//.label(function (p) {
// return p.key;
//})
.renderTitle(true)
.title(function (p) {
return [
"@" + p.key + ":",
"# of Tweets: " + p.value.tweetCount,
"Avg. sentiment: " + p.value.avgSentiment,
"Influence: " + p.value.influence
].join('\n');
})
//.mouseZoomable(true);
dc.renderAll()
}
render_plots()
</script>
<div>
<h1>SocketIO Test</h1>
<form id="fetch_stream" method="POST">
<input type="submit" value="OPEN THE STREAM">
</form>
<form id="stop_fetch" method="POST">
<input type="submit" value="CLOSE THE STREAM">
</form>
<p>Async mode is: <b>{{ async_mode }}</b></p>
<p>Average ping/pong latency: <b><span id="ping-pong"></span>ms</b></p>
<h2>Send:</h2>
<form id="emit" method="POST" action='#'>
<input type="text" name="emit_data" id="emit_data" placeholder="Message">
<input type="submit" value="Echo">
</form>
<form id="broadcast" method="POST" action='#'>
<input type="text" name="broadcast_data" id="broadcast_data" placeholder="Message">
<input type="submit" value="Broadcast">
</form>
<form id="join" method="POST" action='#'>
<input type="text" name="join_room" id="join_room" placeholder="Room Name">
<input type="submit" value="Join Room">
</form>
<form id="leave" method="POST" action='#'>
<input type="text" name="leave_room" id="leave_room" placeholder="Room Name">
<input type="submit" value="Leave Room">
</form>
<form id="send_room" method="POST" action='#'>
<input type="text" name="room_name" id="room_name" placeholder="Room Name">
<input type="text" name="room_data" id="room_data" placeholder="Message">
<input type="submit" value="Send to Room">
</form>
<form id="connect" method="POST" action="#">
<input type="submit" value="Connect">
</form>
<form id="disconnect" method="POST" action="#">
<input type="submit" value="Disconnect">
</form>
<h2>Receive:</h2>
<div id="log"></div>
</div>
</body>
</html>