|
13 | 13 | SUBSCRIBE: 1,
|
14 | 14 | UNSUBSCRIBE: 2,
|
15 | 15 | PUBLISH: 3,
|
16 |
| - ACK: 4, |
17 |
| - CLOSE: 5 |
| 16 | + ACK: 4 |
18 | 17 | };
|
19 | 18 |
|
20 |
| - var retries = 0; |
21 |
| - var ready = false; |
22 | 19 | var queue = [];
|
23 | 20 | var socket = new io.Socket();
|
24 | 21 |
|
25 | 22 | function send(msg) {
|
26 |
| - if (ready) { |
| 23 | + if (socket.isConnected()) { |
27 | 24 | socket.send($.toJSON([msg]));
|
28 | 25 | } else {
|
29 | 26 | queue.push(msg);
|
30 | 27 | }
|
31 | 28 | }
|
32 | 29 |
|
33 |
| - function onConnect() { |
| 30 | + socket.on('connect', function() { |
34 | 31 | console.info('Connected !');
|
35 |
| - ready = true; |
36 | 32 | if (queue.length > 0) {
|
37 | 33 | var q = queue;
|
38 | 34 | queue = [];
|
39 | 35 | console.debug('onConnect - dequeuing: ', $.toJSON(q));
|
40 | 36 | socket.send($.toJSON(q));
|
41 | 37 | }
|
42 |
| - } |
| 38 | + }); |
43 | 39 |
|
44 |
| - function onMessage(mtype, obj, error) { |
| 40 | + socket.on('disconnect', function (disconnectReason, errorMessage) { |
| 41 | + console.debug('onDisconnect - reason=' + disconnectReason + ', err=' + errorMessage); |
| 42 | + if (disconnectReason != socket.DR_CLOSED && disconnectReason != socket.DR_CLOSED_REMOTELY) { |
| 43 | + console.debug('Reconnecting in 10 seconds...'); |
| 44 | + setTimeout(function() { |
| 45 | + console.info('Reconnecting...'); |
| 46 | + socket.connect(); |
| 47 | + }, 10000); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + socket.on('message', function(mtype, obj, error) { |
45 | 52 | console.debug('onMessage - type=' + mtype + ', err=' + error + ', data=' + $.toJSON(obj));
|
46 | 53 | var msg = $.parseJSON(obj);
|
47 | 54 | if (msg.type == MessageType.PUBLISH) {
|
48 | 55 | $('body').append('<p>Firing JSON data ' + msg.data + ' in topic ' + msg.topic + '</p>');
|
49 | 56 | }
|
50 |
| - } |
| 57 | + }); |
51 | 58 |
|
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 | 59 |
|
64 | 60 | window.TESTER = {
|
65 | 61 | socket: socket,
|
66 | 62 | start: function() {
|
67 |
| - if (!ready) { |
| 63 | + if (!socket.isConnected()) { |
68 | 64 | console.info('Starting...');
|
69 |
| - socket.on('connect', onConnect); |
70 |
| - socket.on('disconnect', onDisconnect); |
71 |
| - socket.on('message', onMessage); |
72 | 65 | socket.connect();
|
73 | 66 | }
|
74 | 67 | },
|
75 | 68 | stop: function() {
|
76 |
| - if (ready) { |
77 |
| - ready = false; |
| 69 | + if (socket.isConnected()) { |
78 | 70 | 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(); |
| 71 | + socket.close(); |
86 | 72 | }
|
87 | 73 | },
|
88 | 74 | publish: function(topic, data) {
|
|
113 | 99 | </script>
|
114 | 100 | </head>
|
115 | 101 | <body>
|
116 |
| -<p><strong>In the Firebug console, execute:</strong></p> |
| 102 | +<p><strong>In the Firebug console, you can execute:</strong></p> |
117 | 103 | <pre>TESTER.start();</pre>
|
118 |
| -Firefox connects... And you see the ping pong exchanges each 15 seconds. Then execute: |
119 | 104 | <pre>TESTER.subscribe('myTopic');</pre>
|
120 |
| -Then: |
121 | 105 | <pre>TESTER.publish('myTopic', 'myMessage');</pre>
|
122 |
| -You should see the message appended below this page. Then execute: |
123 | 106 | <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 | 107 | <hr/>
|
134 | 108 | </body>
|
135 | 109 | </html>
|
0 commit comments