Scott Mattocks | @scottmattocks | scott@crisscott.com
The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or <iframe>s and long polling). - WebSocket Protocol Draft 07
I don't have a real time example for this one.
People don't really want to expose that information.
When money is on the line, a 30 second delay could be very expensive.
It's a bit early for a live demo
var url = 'ws://ws.example.com/updates'; var socket = new WebSocket(url);
An object with these properties
socket.onopen=function() { // Process any commands that queued up // while we were connecting.vari= 0,size=this.queue.length;for(i;i<size;i++) {this.send(this.queue[i]); } // Clear the command queue.this.queue= []; };
socket.onmessage=function(evt) { // Process the message from the server$('#datalist').appendChild(evt.data); };
socket.onerror=function() { // Something bad happened. Log a message.console.log('WebSocket error'); };
socket.onclose=function(evt) { // Check to see if the close was clean.if(!evt.wasClean) {console.log('WebSocket error'); } };
The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or <iframe>s and long polling). - WebSocket Protocol Draft 06
socket.send('This message is awesome!');
socket.close();
GET /demo HTTP/1.1 Host: example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: AQIDBAUGBwgJCgsMDQ4PEC== Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Version: 8
HTTP/1.1 101 Switching Protocols Upgrade: WebSocket Connection: Upgrade Sec-WebSocket-Accept: OfS0wDaT5NoxF2gqm7Zj2YtetzM=
$key='AQIDBAUGBwgJCgsMDQ4PEC==';$magic='258EAFA5-E914-47DA-95CA-C5AB0DC85B11';$sha1=sha1($key.$magic,true);$accept=base64_encode($sha1);
| 0 | 2 | 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 I N |
R S V 1 |
R S V 2 |
R S V 3 |
opcode (4) |
M A S K |
Payload len (7) |
Extended payload length (16/63) (if payload len==126/127) |
||||||||||||||||||||||||
| Extended payload length continued, if payload len == 127 | |||||||||||||||||||||||||||||||
| Masking-key, if MASK set to 1 | |||||||||||||||||||||||||||||||
| Masking-key (continued) | |||||||||||||||||||||||||||||||
| Payload Data | |||||||||||||||||||||||||||||||