Skip to content

Instantly share code, notes, and snippets.

@adamrneary
Last active August 29, 2015 14:27
Show Gist options
  • Save adamrneary/f9689178c734c67b029e to your computer and use it in GitHub Desktop.
Save adamrneary/f9689178c734c67b029e to your computer and use it in GitHub Desktop.
// If all goes well:
// 1. The connect action triggers an onopen event (immediately)
// 2. The onOpenHandler sends an identification message (immediately)
// 3. The server returns a ping (immediately)
//
// If the connect action returns an immediate error, CONNECT_ERROR fires.
//
// In some cases, however, no open or close event is ever received from the
// server, so we need to fire a checkConnection event shortly after
// connecting.
exports.connect = function(host, token, resetTimer) {
try {
let socket = new WebSocketProvider(host, token, true).socket;
flux.dispatch(actionTypes.CONNECT, {
readyState: socket.readyState,
resetTimer: resetTimer
});
socket.onopen = openHandler;
socket.onclose = closeHandler;
socket.onmessage = (message) => { messageHandler(message); };
setTimeout(checkConnection, SHORT_DELAY);
} catch(exception) {
flux.dispatch(actionTypes.CONNECT_ERROR, { exception });
setTimeout(checkConnection, SHORT_DELAY);
}
};
exports.disconnect = function() {
const webSocketProvider = new WebSocketProvider();
let socket = webSocketProvider.socket;
socket.onclose = function () {};
socket.close();
webSocketProvider.stopTimer(IDENTIFY_TIMER_NAME);
webSocketProvider.stopTimer(RECONNECT_TIMER_NAME);
flux.dispatch(actionTypes.DISCONNECT, {readyState: socket.readyState});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment