Weitere ähnliche Inhalte Ähnlich wie Sometimes web sockets_dont_work (20) Kürzlich hochgeladen (20) Sometimes web sockets_dont_work5. var websocket = new WebSocket("ws://example.com");
websocket.onopen = function() {
websocket.send("Howdy!");
};
websocket.onmessage = function(message) {
console.log("BEEP", message.data);
};
17. STRATEGIES SHOULD BE
1.quick and effective
2.easy to understand
3.also easy to test
4.simple to modify
18. BACK TO OUR PROBLEMS
1.ancient browsers
2.corporate firewalls
3.restrictive proxies
22. BACK TO OUR PROBLEMS
1.ancient browsers
2.corporate firewalls
3.restrictive proxies
23. FLASH & FIREWALLS
neeeds port 843 to be open or…
waits 3s before trying original port
…
you never know what’s coming
24. FLASH IS NOT THAT BAD
1.it’s basically a WebSocket
2.has low message latency
3.requires less infrastructure
25. DIFFERENT SOLUTIONS
1.give up and avoid Flash
2.wait and switch to HTTP
3.try Flash & HTTP in parallel
1.use first connected
2.switch to Flash
27. BACK TO OUR PROBLEMS
1.ancient browsers
2.corporate firewalls
3.restrictive proxies
30. DEALING WITH IT
1.handle WebSockets like Flash
2.use encrypted connections
3.detect broken connections
4.disable transports temporarily
32. STRATEGIES - RECAP
detect browser features cache transport info
connect in parallel detect disconnections
retry attempts disable transports
support delays support timeouts
35. var runner = strategy.connect(function(error, connection) {
if (error) {
console.log(":(");
} else {
// we can even get multiple connections
console.log("We’ve got a connection!");
}
});
// ok, it has been long enough, I’m giving up
runner.abort();
36. COMPOSING STRATEGIES
strategies form trees
they can use other strategies
decisions are still simple and local
37. STRATEGIES
1.transport provides transport to strategy adapter
2.if runs a test and choses another strategy
3.best connected runs in parallel to find best strategy
4.sequential runs strategies sequentially
5.delayed runs a strategy with a delay
6.cached stores and fetches cached transport info
7.first connected terminates a strategy on first connection
40. sequential sequential
transport transport
ws sockjs
42. best connected
delayed
sequential sequential
transport transport
ws sockjs
43. if
ws.isSupported()
true
best connected
delayed
sequential sequential
transport transport
ws sockjs
44. if
ws.isSupported()
true false
best connected sequential
delayed transport
sequential sequential
sockjs
transport transport
ws sockjs
45. cached
if
ws.isSupported()
true false
best connected sequential
delayed transport
sequential sequential
sockjs
transport transport
ws sockjs
48. SENDING JS IS NOT AN OPTION
1.it’s dangerous
2.too expressive
3.difficult to test
52. [
[":def", "ws_options", {
hostUnencrypted: Pusher.host + ":" + Pusher.ws_port,
hostEncrypted: Pusher.host + ":" + Pusher.wss_port,
lives: 2
}],
[":def", "sockjs_options", {
hostUnencrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_http_port,
hostEncrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_https_port
}],
[":def", "timeouts", {
loop: true,
timeout: 15000,
timeoutLimit: 60000
}],
[":def_transport", "ws", "ws", 3, ":ws_options"],
[":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"],
[":def", "ws_loop", [":sequential", ":timeouts", ":ws"]],
[":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]],
[":def", "strategy",
[":cached", 1800000,
[":if", [":is_supported", ":ws"], [
":best_connected_ever", ":ws_loop", [":delayed", 2000, [":sockjs_loop"]]
], [
":sockjs_loop"
]
]]
]
]
]
53. [
[":def", "ws_options", {
hostUnencrypted: Pusher.host + ":" + Pusher.ws_port,
hostEncrypted: Pusher.host + ":" + Pusher.wss_port,
lives: 2
}],
[":def", "sockjs_options", {
hostUnencrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_http_port,
hostEncrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_https_port
}],
[":def", "timeouts", {
loop: true,
timeout: 15000,
timeoutLimit: 60000
}],
[":def_transport", "ws", "ws", 3, ":ws_options"],
[":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"],
[":def", "ws_loop", [":sequential", ":timeouts", ":ws"]],
[":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]],
[":def", "strategy",
[":cached", 1800000,
[":if", [":is_supported", ":ws"], [
":best_connected_ever", ":ws_loop", [":delayed", 2000, [":sockjs_loop"]]
], [
":sockjs_loop"
]
]]
]
]
]
54. [
[":def", "ws_options", {
hostUnencrypted: Pusher.host + ":" + Pusher.ws_port,
hostEncrypted: Pusher.host + ":" + Pusher.wss_port,
lives: 2
}],
[":def", "sockjs_options", {
hostUnencrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_http_port,
hostEncrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_https_port
}],
[":def", "timeouts", {
loop: true,
timeout: 15000,
timeoutLimit: 60000
}],
[":def_transport", "ws", "ws", 3, ":ws_options"],
[":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"],
[":def", "ws_loop", [":sequential", ":timeouts", ":ws"]],
[":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]],
[":def", "strategy",
[":cached", 1800000,
[":if", [":is_supported", ":ws"], [
":best_connected_ever", ":ws_loop", [":delayed", 2000, [":sockjs_loop"]]
], [
":sockjs_loop"
]
]]
]
]
]
55. [
[":def", "ws_options", {
hostUnencrypted: Pusher.host + ":" + Pusher.ws_port,
hostEncrypted: Pusher.host + ":" + Pusher.wss_port,
lives: 2
}],
[":def", "sockjs_options", {
hostUnencrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_http_port,
hostEncrypted: Pusher.sockjs_host + ":" + Pusher.sockjs_https_port
}],
[":def", "timeouts", {
loop: true,
timeout: 15000,
timeoutLimit: 60000
}],
[":def_transport", "ws", "ws", 3, ":ws_options"],
[":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"],
[":def", "ws_loop", [":sequential", ":timeouts", ":ws"]],
[":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]],
[":def", "strategy",
[":cached", 1800000,
[":if", [":is_supported", ":ws"], [
":best_connected_ever", ":ws_loop", [":delayed", 2000, [":sockjs_loop"]]
], [
":sockjs_loop"
]
]]
]
]
]