autoreconnect with backoff and bug fixes

This commit is contained in:
Gene S
2016-03-16 19:16:31 -07:00
parent e28655a64c
commit e06612f5b3
+61 -21
View File
@@ -190,7 +190,7 @@
var LoginView = React.createClass({ var LoginView = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
login: '', login: this.props.login,
password: '', password: '',
errorCleared: false errorCleared: false
}; };
@@ -526,7 +526,8 @@
errorText={this.props.errorText} /> errorText={this.props.errorText} />
{this.props.state === 'login' ? {this.props.state === 'login' ?
<LoginView disabled={this.props.loginDisabled} <LoginView login={this.props.login}
disabled={this.props.loginDisabled}
onLogin={this.handleLoginRequested} onLogin={this.handleLoginRequested}
onClearError={this.handleClearErrorText} /> : onClearError={this.handleClearErrorText} /> :
@@ -1155,7 +1156,7 @@
); );
} else { } else {
component = ( component = (
<MessagesView.Dummy hideSelf={this.props.hideSelf} /> <MessagesView.Dummy hideSelf={this.props.hideSelf} serverVersion={this.props.serverVersion} />
); );
} }
return component; return component;
@@ -1214,12 +1215,14 @@
MessagesView.Dummy = React.createClass({ MessagesView.Dummy = React.createClass({
render: function() { render: function() {
var version = document.lastModified; var version = document.lastModified;
var serverVersion = this.props.serverVersion ? ("Server: " + this.props.serverVersion) : null;
return ( return (
<div id="dummy-view" className={this.props.hideSelf ? 'nodisplay' : null}> <div id="dummy-view" className={this.props.hideSelf ? 'nodisplay' : null}>
<a href="https://github.com/tinode/chat/"> <a href="https://github.com/tinode/chat/">
<img id="logo" alt="logo" src="img/logo.svg" /> <img id="logo" alt="logo" src="img/logo.svg" />
<h2>Tinode Demo Chat</h2> <h2>Tinode Demo Chat</h2>
<p>Version: {version}</p> <p>Client: {version}</p>
<p>{serverVersion}</p>
</a> </a>
</div> </div>
); );
@@ -1274,18 +1277,22 @@
sidePanelAvatar: null, sidePanelAvatar: null,
dialogSelected: null, dialogSelected: null,
contextMenuVisible: false, contextMenuVisible: false,
login: null,
password: null,
loggedIn: false, loggedIn: false,
errorText: '', errorText: '',
topicSelected: '', topicSelected: '',
loginDisabled: false, loginDisabled: false,
subscriptions: {}, subscriptions: {},
displayMobile: (window.innerWidth <= MEDIA_BREAKPOINT), displayMobile: (window.innerWidth <= MEDIA_BREAKPOINT),
mobilePanel: 'sidepanel' mobilePanel: 'sidepanel',
serverVersion: 'no connection'
}; };
}, },
componentDidMount: function() { componentDidMount: function() {
window.addEventListener('resize', this.handleResize); window.addEventListener('resize', this.handleResize);
Tinode.enableLogging(true, true); Tinode.enableLogging(true, true);
Tinode.onConnect = this.handleConnected;
Tinode.onDisconnect = this.handleDisconnect; Tinode.onDisconnect = this.handleDisconnect;
// Setup with the default transport, usually websocket. // Setup with the default transport, usually websocket.
Tinode.setup(APP_NAME, HOST, API_KEY); Tinode.setup(APP_NAME, HOST, API_KEY);
@@ -1299,6 +1306,42 @@
this.setState({displayMobile: mobile}); this.setState({displayMobile: mobile});
} }
}, },
handleConnected: function(code, text, params) {
if (params) {
this.setState({serverVersion: params.ver + " (" + (params.build ? params.build : "none") + ")"});
}
this.doLogin(this.state.login, this.state.password);
},
doLogin: function(login, password) {
var instance = this;
// Try t login with token. If token is not available, try password. If no password, ask for it.
var promise = null;
var token = Tinode.getLoginToken();
if (password) {
this.setState({password: null});
promise = Tinode.loginBasic(login, password);
} else if (token) {
promise = Tinode.loginToken(token.token);
} else {
this.setState({loginDisabled: false});
}
if (promise) {
this.setState({errorText: ''});
promise.then(function() {
// Logged in fine, subscribe to 'me' attaching callbacks from the contacts view.
var me = Tinode.getMeTopic();
me.onMetaDesc = instance.tnMeMetaDesc;
instance.setState({sidePanelSelected: "contacts", loggedIn: true});
// Subscribe, fetch topic desc, the list of subscriptions, and messages (invites).
me.subscribe({get: {desc: {}, sub: {}, data: {}}});
}).catch(function(err) {
// Login failed, report error
instance.setState({loginDisabled: false, errorText: err.message});
});
}
},
handleDisconnect: function() { handleDisconnect: function() {
this.setState({ this.setState({
sidePanelSelected: 'login', sidePanelSelected: 'login',
@@ -1314,27 +1357,22 @@
contextMenuVisible: false, contextMenuVisible: false,
contextMenuPosition: {top: 0, left: 0}, contextMenuPosition: {top: 0, left: 0},
contextMenuItems: null, contextMenuItems: null,
handleContextMenuClick: undefined handleContextMenuClick: undefined,
serverVersion: 'no connection'
}); });
}, },
// User clicked Login button in the side panel. // User clicked Login button in the side panel.
handleLoginRequest: function(login, password) { handleLoginRequest: function(login, password) {
var instance = this; var instance = this;
this.setState({loginDisabled: true, errorText: ''}); this.setState({loginDisabled: true, errorText: '', login: login, password: password});
Tinode.connect().then(function() { if (Tinode.isConnected()) {
// Login this.doLogin(login, password);
return Tinode.loginBasic(login, password); } else {
}).then(function() { Tinode.connect().catch(function(err) {
// Logged in fine, subscribe to 'me' attaching callbacks from the contacts view. // Socket error
var me = Tinode.getMeTopic(); instance.setState({loginDisabled: false, errorText: err.message});
me.onMetaDesc = instance.tnMeMetaDesc; });
instance.setState({sidePanelSelected: "contacts", loggedIn: true}); }
// Subscribe, fetch topic desc, the list of subscriptions, and messages (invites).
me.subscribe({get: {desc: {}, sub: {}, data: {}}});
}).catch(function(err) {
// Login failed, report error
instance.setState({loginDisabled: false, errorText: err.message});
});
}, },
tnMeMetaDesc: function(desc) { tnMeMetaDesc: function(desc) {
this.setState({ this.setState({
@@ -1480,6 +1518,7 @@
title={this.state.sidePanelTitle} title={this.state.sidePanelTitle}
avatar={this.state.sidePanelAvatar} avatar={this.state.sidePanelAvatar}
subscriptions={this.state.subscriptions} subscriptions={this.state.subscriptions}
login={this.state.login}
loginDisabled={this.state.loginDisabled} loginDisabled={this.state.loginDisabled}
errorText={this.state.errorText} errorText={this.state.errorText}
topicSelected={this.state.topicSelected} topicSelected={this.state.topicSelected}
@@ -1499,6 +1538,7 @@
displayMobile={this.state.displayMobile} displayMobile={this.state.displayMobile}
hideSelf={(this.state.displayMobile && this.state.mobilePanel !== 'topic-view')} hideSelf={(this.state.displayMobile && this.state.mobilePanel !== 'topic-view')}
topic={this.state.topicSelected} topic={this.state.topicSelected}
serverVersion={this.state.serverVersion}
onData={this.tnData} onData={this.tnData}
showContextMenu={this.handleShowContextMenu} showContextMenu={this.handleShowContextMenu}
sendMessage={this.handleSendMessage} /> sendMessage={this.handleSendMessage} />