Skip to content

Commit

Permalink
improving eventbus sample
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Feb 5, 2011
1 parent 2722d43 commit cbfc338
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 53 deletions.
Expand Up @@ -113,7 +113,7 @@ public void onConnect(SocketIOOutbound outbound) {
@Override
public void onDisconnect(DisconnectReason reason, String errorMessage) {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.log(Level.FINE, this + " disconnected.");
LOGGER.log(Level.FINE, this + " disconnected: reason=" + reason);
this.outbound = null;
for (Endpoints ee : subscriptions.values())
ee.remove(this);
Expand Down Expand Up @@ -158,10 +158,6 @@ public void onMessage(int messageType, String message) {
ee.fire(topic, data);
break;
}
case CLOSE: {
close();
break;
}
default: {
close();
throw new IllegalArgumentException("Illegal message: " + message);
Expand Down Expand Up @@ -249,7 +245,6 @@ void fire(String topic, String data) {
private static enum MessageType {

ACK(4),
CLOSE(5),
SUBSCRIBE(1),
UNSUBSCRIBE(2),
PUBLISH(3),
Expand Down
68 changes: 21 additions & 47 deletions samples/eventbus/src/main/webapp/index.html
Expand Up @@ -13,76 +13,62 @@
SUBSCRIBE: 1,
UNSUBSCRIBE: 2,
PUBLISH: 3,
ACK: 4,
CLOSE: 5
ACK: 4
};

var retries = 0;
var ready = false;
var queue = [];
var socket = new io.Socket();

function send(msg) {
if (ready) {
if (socket.isConnected()) {
socket.send($.toJSON([msg]));
} else {
queue.push(msg);
}
}

function onConnect() {
socket.on('connect', function() {
console.info('Connected !');
ready = true;
if (queue.length > 0) {
var q = queue;
queue = [];
console.debug('onConnect - dequeuing: ', $.toJSON(q));
socket.send($.toJSON(q));
}
}
});

function onMessage(mtype, obj, error) {
socket.on('disconnect', function (disconnectReason, errorMessage) {
console.debug('onDisconnect - reason=' + disconnectReason + ', err=' + errorMessage);
if (disconnectReason != socket.DR_CLOSED && disconnectReason != socket.DR_CLOSED_REMOTELY) {
console.debug('Reconnecting in 10 seconds...');
setTimeout(function() {
console.info('Reconnecting...');
socket.connect();
}, 10000);
}
});

socket.on('message', function(mtype, obj, error) {
console.debug('onMessage - type=' + mtype + ', err=' + error + ', data=' + $.toJSON(obj));
var msg = $.parseJSON(obj);
if (msg.type == MessageType.PUBLISH) {
$('body').append('<p>Firing JSON data ' + msg.data + ' in topic ' + msg.topic + '</p>');
}
}
});

function onDisconnect(disconnectReason, errorMessage) {
console.debug('onDisconnect - reason=' + disconnectReason + ', err=' + errorMessage);
if (ready) {
ready = false;
if (retries < 5) {
retries++;
console.info('Reconnecting (retry=' + retries + ')...');
socket.connect();
}
}
}

window.TESTER = {
socket: socket,
start: function() {
if (!ready) {
if (!socket.isConnected()) {
console.info('Starting...');
socket.on('connect', onConnect);
socket.on('disconnect', onDisconnect);
socket.on('message', onMessage);
socket.connect();
}
},
stop: function() {
if (ready) {
ready = false;
if (socket.isConnected()) {
console.info('Stopping...');
socket.removeEvent('connect', onConnect);
socket.removeEvent('disconnect', onDisconnect);
socket.removeEvent('message', onMessage);
socket.send($.toJSON([
{type: MessageType.CLOSE}
]));
socket.disconnect();
socket.close();
}
},
publish: function(topic, data) {
Expand Down Expand Up @@ -113,23 +99,11 @@
</script>
</head>
<body>
<p><strong>In the Firebug console, execute:</strong></p>
<p><strong>In the Firebug console, you can execute:</strong></p>
<pre>TESTER.start();</pre>
Firefox connects... And you see the ping pong exchanges each 15 seconds. Then execute:
<pre>TESTER.subscribe('myTopic');</pre>
Then:
<pre>TESTER.publish('myTopic', 'myMessage');</pre>
You should see the message appended below this page. Then execute:
<pre>TESTER.stop();</pre>
A message is sent to the server, telling him that we stopped the eventbus. The server then executes outboud.close();<br>
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>
To verify that the '_posting' state is not OK, execute:
<pre>alert(TESTER.socket.transport._posting)</pre>
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>
Try to reconnect:
<pre>TESTER.start();</pre>
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/>
After the patch, the post of the pong reply is done correclty.
<hr/>
</body>
</html>

0 comments on commit cbfc338

Please sign in to comment.