|
| 1 | +<!-- |
| 2 | +
|
| 3 | + The MIT License |
| 4 | + Copyright (c) 2010 Tad Glines |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +
|
| 24 | +--> |
1 | 25 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
2 | 26 | "http://www.w3.org/TR/html4/loose.dtd">
|
3 | 27 | <html>
|
|
13 | 37 | SUBSCRIBE: 1,
|
14 | 38 | UNSUBSCRIBE: 2,
|
15 | 39 | PUBLISH: 3,
|
16 |
| - ACK: 4, |
17 |
| - CLOSE: 5 |
| 40 | + ACK: 4 |
18 | 41 | };
|
19 | 42 |
|
20 |
| - var retries = 0; |
21 |
| - var ready = false; |
22 | 43 | var queue = [];
|
23 | 44 | var socket = new io.Socket();
|
24 | 45 |
|
25 | 46 | function send(msg) {
|
26 |
| - if (ready) { |
| 47 | + if (socket.isConnected()) { |
27 | 48 | socket.send($.toJSON([msg]));
|
28 | 49 | } else {
|
29 | 50 | queue.push(msg);
|
30 | 51 | }
|
31 | 52 | }
|
32 | 53 |
|
33 |
| - function onConnect() { |
| 54 | + socket.on('connect', function() { |
34 | 55 | console.info('Connected !');
|
35 |
| - ready = true; |
36 | 56 | if (queue.length > 0) {
|
37 | 57 | var q = queue;
|
38 | 58 | queue = [];
|
39 | 59 | console.debug('onConnect - dequeuing: ', $.toJSON(q));
|
40 | 60 | socket.send($.toJSON(q));
|
41 | 61 | }
|
42 |
| - } |
| 62 | + }); |
43 | 63 |
|
44 |
| - function onMessage(mtype, obj, error) { |
| 64 | + socket.on('disconnect', function (disconnectReason, errorMessage) { |
| 65 | + console.debug('onDisconnect - reason=' + disconnectReason + ', err=' + errorMessage); |
| 66 | + if (disconnectReason != socket.DR_CLOSED && disconnectReason != socket.DR_CLOSED_REMOTELY) { |
| 67 | + console.debug('Reconnecting in 10 seconds...'); |
| 68 | + setTimeout(function() { |
| 69 | + console.info('Reconnecting...'); |
| 70 | + socket.connect(); |
| 71 | + }, 10000); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + socket.on('message', function(mtype, obj, error) { |
45 | 76 | console.debug('onMessage - type=' + mtype + ', err=' + error + ', data=' + $.toJSON(obj));
|
46 | 77 | var msg = $.parseJSON(obj);
|
47 | 78 | if (msg.type == MessageType.PUBLISH) {
|
48 | 79 | $('body').append('<p>Firing JSON data ' + msg.data + ' in topic ' + msg.topic + '</p>');
|
49 | 80 | }
|
50 |
| - } |
| 81 | + }); |
51 | 82 |
|
52 |
| - function onDisconnect(disconnectReason, errorMessage) { |
53 |
| - console.debug('onDisconnect - reason=' + disconnectReason + ', err=' + errorMessage); |
54 |
| - if (ready) { |
55 |
| - ready = false; |
56 |
| - if (retries < 5) { |
57 |
| - retries++; |
58 |
| - console.info('Reconnecting (retry=' + retries + ')...'); |
59 |
| - socket.connect(); |
60 |
| - } |
61 |
| - } |
62 |
| - } |
63 | 83 |
|
64 | 84 | window.TESTER = {
|
65 | 85 | socket: socket,
|
66 | 86 | start: function() {
|
67 |
| - if (!ready) { |
| 87 | + if (!socket.isConnected()) { |
68 | 88 | console.info('Starting...');
|
69 |
| - socket.on('connect', onConnect); |
70 |
| - socket.on('disconnect', onDisconnect); |
71 |
| - socket.on('message', onMessage); |
72 | 89 | socket.connect();
|
73 | 90 | }
|
74 | 91 | },
|
75 | 92 | stop: function() {
|
76 |
| - if (ready) { |
77 |
| - ready = false; |
| 93 | + if (socket.isConnected()) { |
78 | 94 | console.info('Stopping...');
|
79 |
| - socket.removeEvent('connect', onConnect); |
80 |
| - socket.removeEvent('disconnect', onDisconnect); |
81 |
| - socket.removeEvent('message', onMessage); |
82 |
| - socket.send($.toJSON([ |
83 |
| - {type: MessageType.CLOSE} |
84 |
| - ])); |
85 |
| - socket.disconnect(); |
| 95 | + socket.close(); |
86 | 96 | }
|
87 | 97 | },
|
88 | 98 | publish: function(topic, data) {
|
|
113 | 123 | </script>
|
114 | 124 | </head>
|
115 | 125 | <body>
|
116 |
| -<p><strong>In the Firebug console, execute:</strong></p> |
| 126 | +<p><strong>In the Firebug console, you can execute:</strong></p> |
117 | 127 | <pre>TESTER.start();</pre>
|
118 |
| -Firefox connects... And you see the ping pong exchanges each 15 seconds. Then execute: |
119 | 128 | <pre>TESTER.subscribe('myTopic');</pre>
|
120 |
| -Then: |
121 | 129 | <pre>TESTER.publish('myTopic', 'myMessage');</pre>
|
122 |
| -You should see the message appended below this page. Then execute: |
123 | 130 | <pre>TESTER.stop();</pre>
|
124 |
| -A message is sent to the server, telling him that we stopped the eventbus. The server then executes outboud.close();<br> |
125 |
| -In Firefox 3.6 on Ubuntu, the post request is aborted. You can see in the Firebug console the request will be in red.<br> |
126 |
| -To verify that the '_posting' state is not OK, execute: |
127 |
| -<pre>alert(TESTER.socket.transport._posting)</pre> |
128 |
| -The value is true because the aborted request seemed to have stopped the script. That's why if we execute now: socket.connect(); I made sure this value is resetted to false before with the patch<br> |
129 |
| -Try to reconnect: |
130 |
| -<pre>TESTER.start();</pre> |
131 |
| -You'll see a timeout after 15 seconds: the server sends the 3~1~1 ping but the library does not respond, due to the _posting flag being true with the old version of the library.<br/> |
132 |
| -After the patch, the post of the pong reply is done correclty. |
133 | 131 | <hr/>
|
134 | 132 | </body>
|
135 | 133 | </html>
|
0 commit comments