http://mbranko.github.io/webkurs
Ovo je deo web kursa
br. klijenata | bajtova viška | Mbps viška |
---|---|---|
1.000 | 871.000 | 6,6 |
10.000 | 8.710.000 | 66 |
100.000 | 87.100.000 | 667 |
TCPConnection
ws://
i ws://
ws://example.com:8000/chat/
\_/ \_________/\___/ \___/
| | | |
scheme host port server
if (window.WebSocket) { ... } // da li browser podržava
// kreiranje i callbacks
var ws = new WebSocket("ws://www.xyz.com")
ws.onopen = function(event) { };
ws.onclose = function(event) {
alert("closed with status: " + event.code);
};
ws.onmessage = function(event) {
alert("received msg: " + event.data);
};
ws.onerror = function(event) {
alert("error");
};
ws.send("Hello world"); // slanje poruke
ws.close(); // zatvaranje
dostupnost | window.WebSocket ili Modernizr.websocket |
događaji | onopen , onmessage , onclose , onerror |
funkcije | send , close |
atributi | url , readyState , bufferedAmount , ... |
@ServerEndpoint("/echo")
public class EchoServer {
@OnOpen
public void onOpen(Session session){
session.getBasicRemote().sendText("Hello!");
}
@OnMessage
public void onMessage(String message, Session session){
session.getBasicRemote().sendText("Echo: " + message);
}
@OnClose
public void onClose(Session session){
}
}
GET /chat HTTP/1.1
Host: xyz.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Version: 13
Origin: http://xyz.com
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Extensions: ...
Cookie: ...
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
--- (opciono) ---
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Extensions: ...
HTTP | WebSocket | |
---|---|---|
overhead | 100-tine B | 2-6 B |
latency | nova konekcija | ista konekcija |
polling | interval | bez čekanja |
long polling | odgovor-zahtev | bez čekanja |