CSC443: Web Programming

[Pages:11]CSC443: Web Programming

Lecture 20: Web Sockets

Haidar M. Harmanani

HTML5 WebSocket

? Standardized by IETF in 2011.

? Supported by most major browsers including Google Chrome,

? Internet Explorer, Firefox, Safari and Opera

? Basically, it allows the browser to create a socket client (inside a web page) that connects and talks to a WebSocket server.

Fall 2017

CSC443: Web Programming

2

Why WebSockets?

? HTTP is half-duplex ? HTTP has too much overhead ? Ajax doesn't help

Fall 2017

CSC443: Web Programming

3

HTML5 WebSocket protocol

? WebSockets is a big step forward for HTML

Two-way communication without expensive/annoying clientside polling

Ideal for low-latency persistent connections

o e.g. for real-time Web applications o Only requires 2 bytes of overhead per message

Requires server-side support

o e.g. via JavaScript or Python on the Web server

Fall 2017

CSC443: Web Programming

4

Why WebSocket?

? Low latency client-sever and server-client connections ? Legacy HTTP

Connect, send a request, get a response, disconnect. When sending the next request need to establish the connection again.

All communications are initiated by the client, the server cannot proactively send something to the client.

? WebSocket

full-duplex communication channel over a single always-on TCP connection

Fall 2017

CSC443: Web Programming

5

Why WebSocket?

Fall 2017

CSC443: Web Programming

6

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 ? Stock monitors

Fall 2017

CSC443: Web Programming

7

How Does it Work?

? Client and server communicate by sending messages to each other.

? Server can easily broadcast message to all its clients.

Fall 2017

CSC443: Web Programming

8

How Does it Work?

Fall 2017

CSC443: Web Programming

9

How Does it Work?

? It starts off as a HTTP request, which indicates that it wants to "upgrade" to the WebSocket protocol

? If the server can understand it, then the http connection is switched into a WebSocket connection

? Once connected, data is transmitted (bidirectionally) via frames

Fall 2017

request

CSC443: Web Programming

response

10

WebSocket framing

? Data is transmitted via frames, which may be sent by client or server at any time

Fall 2017

CSC443: Web Programming

11

WebSocket masking

? When the MASK bit is set, a 32-bit mask is used to transform each payload byte

Masking and unmasking algorithms are identical because of XOR operation:

foreach index j in payload: data[j] = data[j] XOR mask[j MOD 4]

Security feature or too much overhead?

Fall 2017

CSC443: Web Programming

12

Using WebSockets

? On the server side, we need a server that supports WebSockets

mod_pywebsocket: a Python-based module for Apache Netty: a Java network framework that includes WebSocket

support node.js: a server-side JavaScript framework that supports

WebSocket server implementation

Fall 2017

CSC443: Web Programming

13

Using WebSockets

? ONnodthee.jsserver side, we need a server that supports WebSockets

o Socket.IO o Engine.IO

? Lower level

C#/.NET (IIS 8 4.5) Fleck

Java Atmosphere

Ruby EM-WebSocket

Fall 2017

CSC443: Web Programming

14

Using WebSocket

? Server side

Recommend to use ws, the WebSocket server implementation for Node.js

o

Fall 2017

CSC443: Web Programming

15

Using WebSocket: Server Side

ws is new socket for the new connection

Fall 2017

CSC443: Web Programming

16

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download