Skip to content

Commit

Permalink
Updated the acequia client to properly perform reconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
prgsmall committed Jul 6, 2012
1 parent 31f3417 commit e1446dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
52 changes: 29 additions & 23 deletions lib/acequiaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,29 @@ AcequiaClient.prototype.removeListener = function (msgName, callback) {
* @param {String} url The URL of the acequia server to connect to
*/
AcequiaClient.prototype.connect = function (url) {
var onload = function (that, uri) {
var onload = (function (that, uri) {
return function (evt) {
uri = (typeof(uri) === "undefined") ? that.uri : uri;
that.socket = io.connect(uri, {reconnect: false});
that.socket.on("connect", objCallback(that, "onConnect"));
if (!that.socket) {
uri = (typeof(uri) === "undefined") ? that.uri : uri;
that.socket = io.connect(uri);
that.socket.on("connect", objCallback(that, "onConnect"));
that.socket.on("reconnect", objCallback(that, "onReconnect"));
that.socket.on("disconnect", objCallback(that, "onDisconnect"));
that.socket.on("message", objCallback(that, "onMessage"));
} else {
that.socket.socket.connect();
}
};
};
})(this, url);

this.getSocketIOJS(onload(this, url));
this.getSocketIOJS(onload);
};

/**
* Sends the disconnect message to the Acequia server.
*/
AcequiaClient.prototype.disconnect = function () {
this.send(msg.MSG_DISCONNECT);
this.socket.disconnect();
};

/**
Expand All @@ -168,32 +175,32 @@ AcequiaClient.prototype.getClients = function () {
* @to {String} to The intended recipient of the message.
*/
AcequiaClient.prototype.send = function (msgName, body, to) {
if (msgName !== msg.MSG_CONNECT && !this.isConnected()) {
console.error("AcequiaClient.send " + msgName + ": client is not connected");
} else {
message = new msg.AcequiaMessage(this.clientName, msgName, body, to),
this.socket.send(message.toString());
}
};
/*
AcequiaClient.prototype.send = function (msgName, body) {
if (msgName !== msg.MSG_CONNECT && !this.isConnected()) {
console.error("AcequiaClient.send " + msgName + ": client is not connected");
} else {
var message = new msg.AcequiaMessage(this.clientName, msgName, body);
message = new msg.AcequiaMessage(this.clientName, msgName, body, to),
this.socket.send(message.toString());
}
};
*/

/**
* Handles the connect event from the WebSocket. This method sends the connect
* message to the Acequia client.
* @param {Event} evt The event object.
*/
AcequiaClient.prototype.onConnect = function (evt) {
this.socket.on("disconnect", objCallback(this, "onDisconnect"));
this.socket.on("message", objCallback(this, "onMessage"));
console.debug("onConnect");
this.send(msg.MSG_CONNECT, this.subscribes);
};

/**
* Handles the reconnect event from the WebSocket. This method sends the connect
* message to the Acequia client.
* NB: This event does not appear to be firing.
* @param {Event} evt The event object.
*/
AcequiaClient.prototype.onReconnect = function (evt) {
console.debug("onReconnect");
this.send(msg.MSG_CONNECT, this.subscribes);
};

Expand All @@ -203,9 +210,8 @@ AcequiaClient.prototype.onConnect = function (evt) {
* @param {Event} evt The event object.
*/
AcequiaClient.prototype.onDisconnect = function (evt) {
// alert("onDisconnect");
// this.setConnected(false);
// this.socket = null;
console.debug("onDisconnect");
this.setConnected(false);
};

/**
Expand Down
8 changes: 7 additions & 1 deletion test/acequiaClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
};

var onconnectionChanged = function (connected) {
$("#connected").html(connected.toString());
$("#connect").button("option", "disabled", connected);
$("#disconnect").button("option", "disabled", !connected);
$("#send").button("option", "disabled", !connected);
};

Expand All @@ -52,17 +54,21 @@
$(document).ready( function(){
$("button").button();
$("#send").button("option", "disabled", true);
$("#disconnect").button("option", "disabled", true);
});
</script>
</head>

<body>
<table>
<tr>
<td>Connected: <span id="connected">false</span></td>
</tr>
<tr>
<td><button id="connect" onclick="ac.connect();">Connect</button></td>
</tr>
<tr>
<td><button id="connect" onclick="ac.disconnect();">Disconnect</button></td>
<td><button id="disconnect" onclick="ac.disconnect();">Disconnect</button></td>
</tr>
<tr>
<td><button id="send" onclick="sendMessage();">Send Message</button>&nbsp;<input id="sendMessageName" value="test"/></td>
Expand Down

0 comments on commit e1446dc

Please sign in to comment.