90 likes | 283 Views
WebSockets in HTML5. Mark J Collins October 6 th , 2012. Browser – Server Communication. Handshaking. Request (from browser). GET /chat HTTP/1.1 Upgrade: websocket Connection: Upgrade Host: localhost:8100 Origin: http://localhost:29781 Sec- WebSocket -Key: <request key>
E N D
WebSockets in HTML5 Mark J Collins October 6th, 2012
Handshaking Request (from browser) GET /chat HTTP/1.1 Upgrade: websocket Connection: Upgrade Host: localhost:8100 Origin: http://localhost:29781 Sec-WebSocket-Key: <request key> Sec-WebSocket-Version: 13 Response (from server) HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: <response key>
WebSocketProtocol (ws) • Opcodes: • 0x0 indicates this is a continuation frame • 0x1 specifies the payload contains text • 0x2 indicates a binary payload • 0x8 indicates the connection is being closed • 0x9 specifies the message is a ping • 0xA specifies the message is a pong
Masking • For security reasons, all messages from the client should be masked • Messages from the server are not masked • A different masking key is provided in each message • Unmasking is done by XOR-ing the bytes for (inti = 0; i < length; i++) { payload[i] = (byte)(payload[i] ^ masks[i % 4]); }
WebSocket Server Design • WsAgentConnection – manages the communication with an agent application • WsClientConnection – manages the communication with a client application