-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path09-Websockets.html
513 lines (496 loc) · 20.6 KB
/
09-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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>CMPUT 404</title>
<!-- Styling from reveal.js -->
<link rel="stylesheet" href="node_modules/reveal.js/css/reveal.css">
<link id="revealtheme" rel="stylesheet" href="">
<!-- Theme used for syntax highlighting of code -->
<link id="highlighttheme" rel="stylesheet" href="">
<!-- Custom Styling -->
<link rel="stylesheet" href="cmput404-slides.css">
<link id="404theme" rel="stylesheet" href="">
<!-- Scripts! -->
<script src="node_modules/reveal.js/lib/js/head.min.js"></script>
<script src="node_modules/reveal.js/js/reveal.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script src="node_modules/fitty/dist/fitty.min.js"></script>
<script src="https://twemoji.maxcdn.com/2/twemoji.min.js"></script>
<script src="node_modules/highlightjs/highlight.pack.js"></script>
<script src="fiddler.js"></script><!-- make sure fiddler is last -->
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Anything before this will be sync'd with the other files in the directory if you run ./sync-header-footer.py *.html
HEADER --------------------------
-->
<section>
<h1>CMPUT 404</h1>
<h3>Web Applications and Architecture</h3>
<h2>Part 09: Websockets</h2>
<p>
<small>Created by <br>
<a href="http://softwareprocess.es">Abram Hindle</a>
(<a href="mailto:abram.hindle@ualberta.ca">abram.hindle@ualberta.ca</a>) <br>
and Hazel Campbell (<a href="mailto:hazel.campbell@ualberta.ca">hazel.campbell@ualberta.ca</a>).<br>
Copyright 2014-2019.
</small>
</p>
<p><small>
<button type="button" onClick="whiteStyleSheet()">White Theme</button>
<button type="button" onClick="blackStyleSheet()">Black Theme</button>
</small></p>
</section>
<section>
<h3>Websockets</h3>
<ul>
<li>TCP: Stream-based communcation</li>
<li>UDP: Message-based communication</li>
<li>JavaScript in the browser: Can't do TCP or UDP, but it can do HTTP
<ul>
<li>Maybe that's a good thing... (security)</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>HTTP</h3>
<ul>
<li>Traditionally, you make a request and get a response</li>
<li>Problem: Not nearly as flexible as UDP and TCP</li>
<li>Workarounds:
<ul>
<li>Polling</li>
<li>Long-polling (Comet)</li>
<li>Other tricks</li>
</ul>
</li>
</ul>
</section>
<section>
<ul>
<li>We want to send messages either way whenever</li>
<li>We want them to arrive in order</li>
<li>Solutions:
<ul>
<li>Websockets!</li>
<li>WebRTC</li>
<li>EventSource</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Newer technologies</h3>
<ul>
<li>Websockets!
<ul>
<li>Messages</li>
<li>Full duplex</li>
</ul>
</li>
<li>WebRTC
<ul>
<li>Streaming Media</li>
<li>Messages (same API as Websockets)</li>
</ul>
</li>
<li>EventSource
<ul>
<li>Sever-generated events</li>
<li>Just HTTP</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Websockets</h3>
<ul>
<li>Upgrade existing HTTP connections to a websocket
<ul>
<li>Operate on the same port</li>
<li>Use HTTP to start</li>
<li>Try to be compatible with HTTP proxies</li>
</ul>
</li>
</ul>
</section>
<section>
<h4>Why Websockets</h4>
<ul>
<li>Full-duplex message-based communication
<ul>
<li>Server can push whenever, client can push whenever</li>
<li>Both sides can send data at the same time</li>
<li>Connection stays open</li>
<li>Don't need to poll</li>
<li>Avoid big HTTP headers for each message</li>
<li>Reuse existing technologies as much as possible</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>RFC 6455</h3>
<ul>
<li>Many examples taken from Fette et al. 2011 <a href="https://tools.ietf.org/html/rfc6455">https://tools.ietf.org/html/rfc6455</a></li>
</ul>
</section>
<section style="font-size:90%">
<h3>Websocket Handshake</h3>
<p>Client makes a HTTP request:</p>
<pre><code class="plaintext">
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: soap, wamp
Sec-WebSocket-Version: 13
</code></pre>
<p>Server responds:</p>
<pre><code class="plaintext">
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: wamp
</code></pre>
</section>
<section style="font-size: 90%">
<h3>No Ports</h3>
<ul>
<li>Use GET to specify which Websocket
<ul><li>1 webserver can service multiple websocket services</li>
<li><var>wss://server.example.com/mysocket</var></li>
</ul>
</li>
<li><var>Connection: Upgrade</var>
<ul><li>not <var>keep-alive</var></li></ul>
</li>
<li><var>Upgrade: websocket</var>
<ul><li>Specify the protocol we're upgrading to</li></ul>
</li>
<li>Optional <var>Sec-WebSocket-Protocol: wamp</var>
<ul><li>Specify the <em>sub-</em>protocol: there are some pre-made subprotocols if you don't want to use raw websockets</li></ul>
</li>
</ul>
</section>
<section style="font-size: 90%">
<h3>Key and Accept</h3>
<ul>
<li>Brown M&M test</li>
<li>Client sends <var>Sec-WebSocket-Key</var> with a random string</li>
<li>Server</li>
<ul>
<li>takes the key, appends <var>258EAFA5-E914-47DA-95CA-C5AB0DC85B11</var></li>
<li>Takes the SHA1 SUM</li>
<li>Base64-encodes the SHA1 SUM</li>
<li>Sends that with the <var>Sec-WebSocket-Accept</var> header</li>
</ul>
</li>
<li>Client knows for sure the server's ready for websocket</li>
</ul>
</section>
<section style="font-size: 90%">
<ul>
<li>Request:
<ul>
<li><var>Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==</var></li>
</ul>
</li>
<li>Response:
<ul>
<li><var>Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=</var></li>
<li>Magic string: <var>258EAFA5-E914-47DA-95CA-C5AB0DC85B11</var></li>
<li><var>base64(sha1SumBinary(key+guid))</var></li>
</ul>
</li>
</ul>
<pre><code type="plaintext">
$ echo -en dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11 \
| sha1sum
b37a4f2cc0624f1690f64606cf385945b2bec4ea -
$ base64 -d | hexdump -C
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
00000000 b3 7a 4f 2c c0 62 4f 16 90 f6 46 06 cf 38 59 45 |.zO,.bO...F..8YE|
00000010 b2 be c4 ea |....|
00000014
</code></pre>
</section>
<section>
<h3>In WebSocket Protocol</h3>
<ul>
<li>Client and server exchange "frames"
<ul>
<li>Control frames (out of band)
<ul>
<li>Close connection <var>0x8</var></li>
<li>Ping <var>0x9</var> and Pong <var>0xA</var></li>
</ul>
</li>
<li>Data frames
<ul>
<li>These are the messages, updates, events, RPCs, whatever you're exchanging with the server</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Messages</h3>
<ul>
<li>Sent in data frames</li>
<li>Could be text or binary</li>
<li>Size is known ahead of time
<ul>
<li>efficent buffering</li>
</ul>
</li>
<li>Can be fragmented into multiple data frames</li>
</ul>
</section>
<section>
<h3>Frames</h3>
<pre><code class="plaintext">
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
</code></pre>
<ul>
<li><var>FIN</var> is final fragment in a single message</var></li>
</ul>
</section>
<section>
<h3>Compact binary header</h3>
<ul>
<li>Byte 1: FIN bit, 3 reserved bits, 4-bit opcode</li>
<li>Byte 2: Mask (enables XOR "encryption" mask) and payload len</li>
<li>If payload len = 126, 16-bit payload len follows</li>
<li>If payload len = 127, 64-bit payload len follows</li>
<li>If maks is set, the masking key follows</li>
<li>Finally the data</li>
</ul>
</section>
<section>
<ul>
<li>Smallest fragment: 1byte message, no mask = 4 bytes!</li>
<li>Largest possible header: 14 bytes</li>
<li>Each message: just send fragments until FIN</li>
<li>Don't need to worry about ordering: TCP is handling that</li>
</ul>
</section>
<section>
<h3>Opcodes</h3>
<ul>
<li><var>0x1</var> UTF-8 text</var> (don't break characters)</li>
<li><var>0x2</var> binary</var></li>
<li><var>0x8</var> close</var></li>
<li><var>0x9</var> ping</var></li>
<li><var>0xA</var> pong</var></li>
</ul>
</section>
<section>
<!-- <p>First byte?</var>
<ul>
<li><var>0x81</var> final fragment of a text message</var></li>
<li><var>0x02</var> binary message to be continued in another fragment</var></li>
</ul>-->
<p>Example:</p>
<ul>
<li><var>0x01 0x03 0x48 0x65 0x6c</var> "Hel"...</var>
<ul>
<li>What does <var>0x01</var> mean?</li>
</ul>
</li>
<li><var>0x80 0x02 0x6c 0x6f</var> ..."lo"</var>
<ul>
<li>What does <var>0x80</var> mean?</li>
</ul>
</li>
</ul>
</section>
<section style="font-size: 85%">
<p>Why "mask"?</p>
<ul>
<li>Not for privacy (encryption)</li>
<li>Prevent accidentally being parsed as HTTP!</li>
<li>Websockets are supposed to work with existing infrastructure</li>
<li>Maintainers worried about cache poisoning by sending fake looking
GET requests over websockets. – Bad proxy servers etc.</li>
<li>Masking encodes and garbles a frame with a mask so that you can't
send a GET request in the plain</li>
<li>Allows <em>browsers</em> to protect against malicious pages doing
bad things they shouldn't
<ul>
<li>Of course we can't do anything about custom clients, which can send whatever they want over TCP</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>WebSocket URIs</h3>
<ul>
<li>You can use <var>ws://yourserver.com:9090/websockethandler/</var></li>
<li><var>wss:</var> is websocket secure
<ul>
<li>Inherits TLS from the HTTPS connection used intially</li>
</ul>
</li>
<li>Same format as HTTP URI
<ul>
<li>GET only</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Performance</h3>
<ul>
<li>Better two-way communcation</li>
<li>Missing out on client side caching</li>
<li>Reinvent the wheel (AKA TCP/UDP)</li>
<li>Beat the firewall</li>
<li>Doesn't fully replace XHR/fetch AJAX</li>
</ul>
</section>
<section>
<h3>Errors</h3>
<ul>
<li>Bad UTF-8 encoding → close connection</li>
<li>No real prescription other than to close the connection</li>
<li>Closing is done by control frame, TLS, and TCP close</li>
</ul>
</section>
<section>
<h3>In the Browser</h3>
<ul>
<li>JS code in the browser won't have access to fragments, masking, etc.</li>
<li>Simple browser API:
<ul>
<li>Open</li>
<li>Send and recieve messages</li>
<li>Close</li>
</ul>
</li>
<li>Browser sanitizes everything, is in control to prevent malicious
web pages from exploiting your browser to do things like poison
proxies
</li>
</ul>
</section>
<section>
<h3>WebSocket in JS</h3>
<pre><code class="javascript">
var ws = new WebSocket("ws://www.example.com/socketserver");
var ws = new WebSocket("ws://www.example.com/socketserver", [“proto1”, “proto2”]);
ws.send("A string");
var buffer = new ArrayBuffer(16);
var int32View = new Int32Array(buffer);
websocketInstance.send( int32View ); // send binary
ws.close();
</code></pre>
</section>
<section>
<h3>Examples</h3>
<ul>
<li>in <var>examples/WebSocketsExamples</var>
</li>
<li><a href="https://github.com/uofa-cmput404/cmput404-slides/tree/master/examples/WebSocketsExamples">https://github.com/uofa-cmput404/cmput404-slides/tree/master/examples/WebSocketsExamples</a></li>
<li>Example chat app from Mozilla: <a href="https://github.com/mdn/samples-server/tree/master/s/websocket-chat">https://github.com/mdn/samples-server/tree/master/s/websocket-chat</a></li>
</ul>
<cite>Examples from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays</a>
and <a href="https://developer.mozilla.org/en/docs/WebSockets/Writing_WebSocket_client_applications">https://developer.mozilla.org/en/docs/WebSockets/Writing_WebSocket_client_applications</a></cite>
</section>
<section>
<h3>Examples</h3>
<ul>
<li><a href="http://www.websocket.org/demos.html">http://www.websocket.org/demos.html</a></li>
<li><a href="https://developer.mozilla.org/en-US/demos/detail/websocket-chat">https://developer.mozilla.org/en-US/demos/detail/websocket-chat</a></li>
<li><a href="https://www.youtube.com/watch?v=Ua-PcbC5xMI">https://www.youtube.com/watch?v=Ua-PcbC5xMI</a></li>
<li><a href="https://crawl.develz.org/play.htm">https://crawl.develz.org/play.htm</a></li>
</ul>
</section>
<section style="font-size: 75%">
<h3>Resources</h3>
<ul>
<li>Mozilla WebSocket dev guide
<ul>
<li><a href="https://developer.mozilla.org/en/docs/WebSockets/Writing_WebSocket_client_applications">https://developer.mozilla.org/en/docs/WebSockets/Writing_WebSocket_client_applications</a></li>
</ul>
</li>
<li>ByteArrays/Typed Arrays in JS
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays</a></li>
</ul>
</li>
<li>Async Interactions Server Side
<ul>
<li><a href="https://github.com/abramhindle/html5-audio-streaming-via-webaudio-and-raw-encoding/blob/master/jack-spammer.pl">https://github.com/abramhindle/html5-audio-streaming-via-webaudio-and-raw-encoding/blob/master/jack-spammer.pl</a></li>
</ul>
</li>
<li>Javascript Example using WebAudio
<ul>
<li><a href="https://github.com/abramhindle/html5-audio-streaming-via-webaudio-and-raw-encoding/blob/616ddc42bd4303fdb7dfe01147588eb205a445ef/jack-spammer.pl">https://github.com/abramhindle/html5-audio-streaming-via-webaudio-and-raw-encoding/blob/616ddc42bd4303fdb7dfe01147588eb205a445ef/jack-spammer.pl</a></li>
</ul>
</li>
</ul>
</section>
<!-- Anything after this will be sync'd with the other files in the directory if you run ./sync-header-footer.py *.html
FOOTER --------------------------
-->
<section style="font-size: 90%">
<h4>License</h4>
<p>Copyright 2014 ⓒ Abram Hindle</p>
<p>Copyright 2019 ⓒ Hazel Victoria Campbell and contributors</p>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creative Commons Licence" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a><br />The textual components and original images of this slide deck are
placed under the Creative Commons is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.
</p>
<p>Other images used under fair use and copyright their copyright holders.</p>
</section>
<section>
<h4>License</h4>
The source code to this slide deck is:
<pre><code class="plaintext">
Copyright (C) 2018 Hakim El Hattab, http://hakim.se, and reveal.js contributors
Copyright (C) 2019 Hazel Victoria Campbell, Abram Hindle and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
</code></pre>
</section>
</div>
</div>
</body>
</html>