websockets [intro]. websockets? full-duplex tcp-based persistent connection message-oriented...

24
WebSockets [intro]

Upload: zoe-johnston

Post on 25-Dec-2015

240 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSockets[intro]

Page 2: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSockets?

• full-duplex• TCP-based• persistent connection• message-oriented• cross-origin• standardized protocol• JavaScript API

Page 3: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSockets in action• http://wordsquared.com/• http://paintwith.me/• http://www.youtube.com/watch?

v=64TcBiqmVko&feature=player_embedded

• …and more!

Page 4: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Why WebSockets?

• HTTP is half-duplex• HTTP has too much overhead• Ajax doesn’t help• Comet doesn’t help

Page 5: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket uses

• Real-time updates (sports, finance)• Games• Collaboration & Education• Feeds & rich communication• Location-based services• Services based on real-time APIs• User Monitoring & Logging

Page 6: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket protocol

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13

HTTPClient

HTTPServer

HTTP GET Upgrade request

Page 7: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket protocol

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat

HTTPClient

HTTPServerHTTP 101 Switching Protocol response

Page 8: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Using WebSockets

Server-side:• Node.js

WebSocket-Node Socket.IO

Engine.IO• C#/.NET (IIS 8 ASP.NET 4.5)

XSockets.NET Fleck

• JavaAtmosphere

• RubyEM-WebSocket

Page 9: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket API browser support

Google Chrome 16

Opera 12.10

Firefox 11

Safari 6

Internet Explorer 10

Page 10: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket API

WebSocket WebSocket( in DOMString url, in optional DOMString protocols);

WebSocket WebSocket( in DOMString url, in optional DOMString[] protocols);

Page 11: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket API

1. Parse URL or throw SYNTAX_ERR2. If port is blocked, throw SECURITY_ERR3. Check sub-protocol4. Get origin from <script>5. Return WebSocket object6. Establish a socket connection

Page 12: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSocket API

• readyState• CONNECTING ( = 0 )• OPEN ( = 1 )• CLOSE ( = 2 )• CLOSING

• onopen • onmessage • onclose • onerror

Status Methods• close(code, reason)• send(data)

• String• ArrayBuffer• Blob

≤123, LOL

https://developer.mozilla.org/en-US/docs/WebSockets/WebSockets_reference/CloseEvent#Status_codes

Page 13: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

var socket = new WebSocket('ws://www.example.com/socketserver', 'protocolOne');

socket.onopen = function() { socket.send('hey');}

socket.onmessage = function(event) { var msg = JSON.parse(event.data);}

WebSocket API

Page 14: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WebSockets Demo

HTTP server

WS server

Server Tablet

Desktop

mobile.html

desktop.html

deviceorientation

Page 15: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data in JS [oh why]

• WebGL• Files• XHR2• WebSockets

• DataView• Typed arrays• Blobs• Clamped arrays

Page 16: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data in JS [is great]

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

= 1 byte

var buffer = new ArrayBuffer(16)

var dv = new DataView(buffer, 8, 4)

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

dv.setUint16(2, 0x1337)

13 37

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f{DataView

Page 17: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data in JS [is great]= 1 byte

13 37

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

dv.getInt8(2) -> 0x13

Int8, Uint8:

Int16, Uint16:

Int32, Uint32, Float32:

Float64:x2

x4

x8

Page 18: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data in JS [is great]= 1 byte

13 37

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

13 37

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

var arr = new Int32Array(buffer)

[0] [1] [2] [3]

arr[2] ?

Page 19: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

WAT

Page 20: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data in JS [is great?..]= 1 byte

13 37

0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f

var arr = new Int32Array(buffer)

[0] [1] [2] [3]13 37

0x08 0x09 0x0a 0x0b

LSB MSB

0x37130000dv.setUint16(2, 0x1337, true)

Page 21: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

BlobsBlob Blob( [optional] Array parts, [optional] BlobPropertyBag properties);

BlobBuilder

Blob slice( optional long long start, optional long long end, optional DOMString contentType};

Page 22: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

Binary data over WS

…just send it already!

opcode meaning

0 Continuation Frame

1 Text Frame

2 Binary Frame

8 Connection Close Frame

9 Ping Frame

10 Pong Frame

socket.binaryType = “arraybuffer” | “blob”

Page 23: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

The WebSocket Challenge

1. ArrayBuffer2. JSON Danger3. { msg:”challenge_accepted”, name: “Socketeers” }4. { msg: “auth”, auth_token: “6f7sd8s78”}5. Task1 request: { msg: “task_one”, auth_token: “6f7sd8s78”}6. Task1 server response: { msg: “compute”, operator:”+/-/*”, operands:[4,5]}7. Task1 send result: { msg: “task_one_result”, result: 9, auth_token: “6f7sd8s78” }8. Task2 request: { msg: ???, auth_token: “6f7sd8s78” }9. Task2 server response: {msg: “binary_sum”, bits: 8/16 } and ArrayBuffer (16 bytes)

Convert to an unsigned typed array according to the `bits` field10. Task2 send result: { msg: “task_two_result”, result: 0, auth_token: “6f7sd8s78” }

Check message type with typeof evt.data == “string”

wsc.jit.su

Page 24: WebSockets [intro]. WebSockets? full-duplex TCP-based persistent connection message-oriented cross-origin standardized protocol JavaScript API

The End[you are awesome]