Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies
-
Upload
business-link-krakow -
Category
Mobile
-
view
191 -
download
3
Transcript of Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies
![Page 2: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/2.jpg)
TCP/IP Socket
http://www.freeimages.co.uk/
![Page 3: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/3.jpg)
Protocol
http://www.freeimages.co.uk/
![Page 4: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/4.jpg)
Protocol: telnet
![Page 5: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/5.jpg)
Protocol: HTTP
![Page 6: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/6.jpg)
Protocol: HTTPS
![Page 7: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/7.jpg)
Protocol: fixed length message
![Page 8: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/8.jpg)
Protocol: message delimiter
![Page 9: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/9.jpg)
Example: Betradar TCP/SSL
@tornado.gen.coroutinedef receive(self): """Read CRLF-delimited messages and push them onto the queue.""" try: if not self.iostream.closed():
message_body = yield self.iostream.read_until('\r\n\r\n')
logger.debug('Received message: %s', message_body.strip())
received_at = datetime.datetime.utcnow() message = parser.parse(message_body, received_at)
if message is not None: if self.is_whitelisted(message): self.messages.enqueue(message) self.log(message)
self.ioloop.add_callback(self.receive)
except tornado.iostream.StreamClosedError:
if not self._closing: logger.error('Betradar connection closed by remote endpoint')
self.heartbeat.stop()
if self.on_closed: self.on_closed()
![Page 10: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/10.jpg)
WebSocket: handshake
![Page 11: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/11.jpg)
WebSocket: server frame
![Page 12: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/12.jpg)
WebSocket: client frame
![Page 13: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/13.jpg)
WebSocket: JavaScript API
<!DOCTYPE html><html><head> <title>WebSocket Demo</title> <script> var ws = new WebSocket("ws://localhost:8080/websocket");
ws.onopen = function(event) { console.log("open"); }; ws.onclose = function(event) { console.log("closed"); }; ws.onerror = function(event) { console.log("error"); }; ws.onmessage = function(event) { console.log(event.data); };
function onButtonClick() { ws.send(document.getElementById("message").value) } </script></head><body> <input type="text" id="message"> <button type="button" onclick="onButtonClick();">Send</button></body></html>
![Page 14: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/14.jpg)
WebSocket: JavaScript libs
var ws = STOMP.over(new SockJS("/websocket"));
var ws = io.connect("http://localhost:8080/websocket");
![Page 15: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/15.jpg)
UNSUBSCRIBE
id:sub-0
\u0000
SUBSCRIBEid:sub-0destination:/topic/chat.all\u0000
WebSocket: sub protocol
CONNEC
T
accept
-versi
on:1.1
,1.0
heart-
beat:1
0000,1
0000
\u0000
CONNECTEDversion:1.1heart-beat:0,0\u0000
MESSAGEsubscription:sub-0message-id:007destination:/topic/chat.allcontent-type:text/plaincontent-length:11
hello world\u0000
SENDdestination:/topic/chat.all
content-type:text/plainhello world\u0000
![Page 16: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/16.jpg)
Debugging
![Page 17: Bartosz Zaczyński (Grand Parade Poland) - WebSocket for Dummies](https://reader034.fdocuments.us/reader034/viewer/2022042706/5882ae3c1a28abd75a8b5927/html5/thumbnails/17.jpg)
Summary
1. Lightweight binary protocol on top of TCP.
2. Different from HTTP (except for the handshake).
3. Replaces AJAX polling and plugins such as Flash.