Skip to content

Commit 544a90d

Browse files
committed
Merge remote branch 'Ovea/master'
2 parents e6fbbec + c81e169 commit 544a90d

File tree

3 files changed

+51
-54
lines changed

3 files changed

+51
-54
lines changed

core/src/main/resources/com/glines/socketio/socket.io.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,11 @@ JSONPPolling.xdomainCheck = function(){
10111011
}
10121012
return null;
10131013
};
1014-
1014+
1015+
Socket.prototype.isConnected = function(){
1016+
return this.socketState == this.CONNECTED;
1017+
};
1018+
10151019
Socket.prototype.connect = function(){
10161020
if (this.socketState != this.CLOSED) throw ("Socket not closed!");
10171021
if (!this.transport) throw ("No available transports!");

samples/eventbus/src/main/java/com/glines/socketio/sample/eventbus/EventBusServlet.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void onConnect(SocketIOOutbound outbound) {
113113
@Override
114114
public void onDisconnect(DisconnectReason reason, String errorMessage) {
115115
if (LOGGER.isLoggable(Level.FINE))
116-
LOGGER.log(Level.FINE, this + " disconnected.");
116+
LOGGER.log(Level.FINE, this + " disconnected: reason=" + reason);
117117
this.outbound = null;
118118
for (Endpoints ee : subscriptions.values())
119119
ee.remove(this);
@@ -158,10 +158,6 @@ public void onMessage(int messageType, String message) {
158158
ee.fire(topic, data);
159159
break;
160160
}
161-
case CLOSE: {
162-
close();
163-
break;
164-
}
165161
default: {
166162
close();
167163
throw new IllegalArgumentException("Illegal message: " + message);
@@ -249,7 +245,6 @@ void fire(String topic, String data) {
249245
private static enum MessageType {
250246

251247
ACK(4),
252-
CLOSE(5),
253248
SUBSCRIBE(1),
254249
UNSUBSCRIBE(2),
255250
PUBLISH(3),
Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
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+
-->
125
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
226
"http://www.w3.org/TR/html4/loose.dtd">
327
<html>
@@ -13,76 +37,62 @@
1337
SUBSCRIBE: 1,
1438
UNSUBSCRIBE: 2,
1539
PUBLISH: 3,
16-
ACK: 4,
17-
CLOSE: 5
40+
ACK: 4
1841
};
1942

20-
var retries = 0;
21-
var ready = false;
2243
var queue = [];
2344
var socket = new io.Socket();
2445

2546
function send(msg) {
26-
if (ready) {
47+
if (socket.isConnected()) {
2748
socket.send($.toJSON([msg]));
2849
} else {
2950
queue.push(msg);
3051
}
3152
}
3253

33-
function onConnect() {
54+
socket.on('connect', function() {
3455
console.info('Connected !');
35-
ready = true;
3656
if (queue.length > 0) {
3757
var q = queue;
3858
queue = [];
3959
console.debug('onConnect - dequeuing: ', $.toJSON(q));
4060
socket.send($.toJSON(q));
4161
}
42-
}
62+
});
4363

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) {
4576
console.debug('onMessage - type=' + mtype + ', err=' + error + ', data=' + $.toJSON(obj));
4677
var msg = $.parseJSON(obj);
4778
if (msg.type == MessageType.PUBLISH) {
4879
$('body').append('<p>Firing JSON data ' + msg.data + ' in topic ' + msg.topic + '</p>');
4980
}
50-
}
81+
});
5182

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-
}
6383

6484
window.TESTER = {
6585
socket: socket,
6686
start: function() {
67-
if (!ready) {
87+
if (!socket.isConnected()) {
6888
console.info('Starting...');
69-
socket.on('connect', onConnect);
70-
socket.on('disconnect', onDisconnect);
71-
socket.on('message', onMessage);
7289
socket.connect();
7390
}
7491
},
7592
stop: function() {
76-
if (ready) {
77-
ready = false;
93+
if (socket.isConnected()) {
7894
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();
8696
}
8797
},
8898
publish: function(topic, data) {
@@ -113,23 +123,11 @@
113123
</script>
114124
</head>
115125
<body>
116-
<p><strong>In the Firebug console, execute:</strong></p>
126+
<p><strong>In the Firebug console, you can execute:</strong></p>
117127
<pre>TESTER.start();</pre>
118-
Firefox connects... And you see the ping pong exchanges each 15 seconds. Then execute:
119128
<pre>TESTER.subscribe('myTopic');</pre>
120-
Then:
121129
<pre>TESTER.publish('myTopic', 'myMessage');</pre>
122-
You should see the message appended below this page. Then execute:
123130
<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.
133131
<hr/>
134132
</body>
135133
</html>

0 commit comments

Comments
 (0)