Skip to content

Commit cbfc338

Browse files
committed
improving eventbus sample
1 parent 2722d43 commit cbfc338

File tree

2 files changed

+22
-53
lines changed

2 files changed

+22
-53
lines changed

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),

samples/eventbus/src/main/webapp/index.html

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,62 @@
1313
SUBSCRIBE: 1,
1414
UNSUBSCRIBE: 2,
1515
PUBLISH: 3,
16-
ACK: 4,
17-
CLOSE: 5
16+
ACK: 4
1817
};
1918

20-
var retries = 0;
21-
var ready = false;
2219
var queue = [];
2320
var socket = new io.Socket();
2421

2522
function send(msg) {
26-
if (ready) {
23+
if (socket.isConnected()) {
2724
socket.send($.toJSON([msg]));
2825
} else {
2926
queue.push(msg);
3027
}
3128
}
3229

33-
function onConnect() {
30+
socket.on('connect', function() {
3431
console.info('Connected !');
35-
ready = true;
3632
if (queue.length > 0) {
3733
var q = queue;
3834
queue = [];
3935
console.debug('onConnect - dequeuing: ', $.toJSON(q));
4036
socket.send($.toJSON(q));
4137
}
42-
}
38+
});
4339

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) {
4552
console.debug('onMessage - type=' + mtype + ', err=' + error + ', data=' + $.toJSON(obj));
4653
var msg = $.parseJSON(obj);
4754
if (msg.type == MessageType.PUBLISH) {
4855
$('body').append('<p>Firing JSON data ' + msg.data + ' in topic ' + msg.topic + '</p>');
4956
}
50-
}
57+
});
5158

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

6460
window.TESTER = {
6561
socket: socket,
6662
start: function() {
67-
if (!ready) {
63+
if (!socket.isConnected()) {
6864
console.info('Starting...');
69-
socket.on('connect', onConnect);
70-
socket.on('disconnect', onDisconnect);
71-
socket.on('message', onMessage);
7265
socket.connect();
7366
}
7467
},
7568
stop: function() {
76-
if (ready) {
77-
ready = false;
69+
if (socket.isConnected()) {
7870
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();
8672
}
8773
},
8874
publish: function(topic, data) {
@@ -113,23 +99,11 @@
11399
</script>
114100
</head>
115101
<body>
116-
<p><strong>In the Firebug console, execute:</strong></p>
102+
<p><strong>In the Firebug console, you can execute:</strong></p>
117103
<pre>TESTER.start();</pre>
118-
Firefox connects... And you see the ping pong exchanges each 15 seconds. Then execute:
119104
<pre>TESTER.subscribe('myTopic');</pre>
120-
Then:
121105
<pre>TESTER.publish('myTopic', 'myMessage');</pre>
122-
You should see the message appended below this page. Then execute:
123106
<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.
133107
<hr/>
134108
</body>
135109
</html>

0 commit comments

Comments
 (0)