OLD | NEW |
1 ### WebSockets {#html-websockets} | 1 ### WebSockets {#html-websockets} |
2 | 2 |
3 A WebSocket allows your web app to exchange data with a server interactively&mda
sh;no | 3 A WebSocket allows your web app to exchange data with a server interactively&mda
sh;no |
4 polling necessary. | 4 polling necessary. |
5 A server creates the WebSocket and listens for requests on | 5 A server creates the WebSocket and listens for requests on |
6 a URL that starts with **ws://**—for example, | 6 a URL that starts with **ws://**—for example, |
7 ws://127.0.0.1:1337/ws. | 7 ws://127.0.0.1:1337/ws. |
8 The data transmitted over a WebSocket can be | 8 The data transmitted over a WebSocket can be |
9 a string, a blob, or an | 9 a string, a blob, or an |
10 [ArrayBuffer](http://api.dartlang.org/html/ArrayBuffer.html). | 10 [ArrayBuffer](http://api.dartlang.org/html/ArrayBuffer.html). |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 Here's an example of a method that | 70 Here's an example of a method that |
71 creates a WebSocket object | 71 creates a WebSocket object |
72 and handles message, open, close, and error events. | 72 and handles message, open, close, and error events. |
73 | 73 |
74 {% highlight dart %} | 74 {% highlight dart %} |
75 connectToWebSocket([int retrySeconds = 2]) { | 75 connectToWebSocket([int retrySeconds = 2]) { |
76 bool encounteredError = false; | 76 bool encounteredError = false; |
77 webSocket = new WebSocket(url); | 77 webSocket = new WebSocket(url); |
78 | 78 |
79 webSocket.on.message.add((e) { | 79 webSocket.on.message.add((e) { |
80 receivedMessage(e.data); | 80 receivedMessage((e as MessageEvent).data); |
81 }); | 81 }); |
82 | 82 |
83 webSocket.on.open.add((e) { | 83 webSocket.on.open.add((e) { |
84 print('Connected'); | 84 print('Connected'); |
85 }); | 85 }); |
86 | 86 |
87 webSocket.on.close.add((e) { | 87 webSocket.on.close.add((e) { |
88 print('web socket closed, retrying in $retrySeconds seconds'); | 88 print('web socket closed, retrying in $retrySeconds seconds'); |
89 if (!encounteredError) { | 89 if (!encounteredError) { |
90 window.setTimeout(() => connectToWebSocket(retrySeconds*2), | 90 window.setTimeout(() => connectToWebSocket(retrySeconds*2), |
91 1000*retrySeconds); | 91 1000*retrySeconds); |
92 } | 92 } |
93 encounteredError = true; | 93 encounteredError = true; |
94 }); | 94 }); |
95 | 95 |
96 webSocket.on.error.add((e) { | 96 webSocket.on.error.add((e) { |
97 chatWindow.displayNotice('Error connecting to ws'); | 97 chatWindow.displayNotice('Error connecting to ws'); |
98 if (!encounteredError) { | 98 if (!encounteredError) { |
99 window.setTimeout(() => connectToWebSocket(retrySeconds*2), | 99 window.setTimeout(() => connectToWebSocket(retrySeconds*2), |
100 1000*retrySeconds); | 100 1000*retrySeconds); |
101 } | 101 } |
102 encounteredError = true; | 102 encounteredError = true; |
103 }); | 103 }); |
104 } | 104 } |
105 {% endhighlight %} | 105 {% endhighlight %} |
OLD | NEW |