Commit a0b97728 authored by Alexander.Trofimov's avatar Alexander.Trofimov

move sockjs.onopen into a separate function _onServerOpen

move sockjs.onclose into a separate function _onServerClose
parent 7cb51d16
......@@ -1535,133 +1535,137 @@
DocsCoApi.prototype._initSocksJs = function () {
var t = this;
var sockjs;
if (window['IS_NATIVE_EDITOR']) {
sockjs = this.sockjs = window['SockJS'];
sockjs.open();
return sockjs;
} else {
//ограничиваем transports WebSocket и XHR / JSONP polling, как и engine.io https://github.com/socketio/engine.io
//при переборе streaming transports у клиента с wirewall происходило зацикливание(не повторялось в версии sock.js 0.3.4)
sockjs = this.sockjs = new (AscCommon.getSockJs())(this.sockjs_url, null,
{'transports': ['websocket', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']});
}
sockjs.onopen = function () {
if (t.reconnectTimeout) {
clearTimeout(t.reconnectTimeout);
t.reconnectTimeout = null;
t.attemptCount = 0;
}
t._state = ConnectionState.WaitAuth;
t.onFirstConnect();
};
sockjs.onmessage = function (e) {
t._onServerMessage(e.data);
};
sockjs.onclose = function (evt) {
if (ConnectionState.SaveChanges === t._state) {
// Мы сохраняли изменения и разорвалось соединение
t._isReSaveAfterAuth = true;
// Очищаем предыдущий таймер
if (null !== t.saveCallbackErrorTimeOutId) {
clearTimeout(t.saveCallbackErrorTimeOutId);
}
}
t._state = ConnectionState.Reconnect;
var bIsDisconnectAtAll = ((c_oCloseCode.serverShutdown <= evt.code && evt.code <= c_oCloseCode.jwtError) ||
t.attemptCount >= t.maxAttemptCount);
var errorCode = null;
if (bIsDisconnectAtAll) {
t._state = ConnectionState.ClosedAll;
errorCode = t._getDisconnectErrorCode(evt.code);
}
if (t.onDisconnect) {
t.onDisconnect(evt.reason, errorCode);
}
//Try reconect
if (!bIsDisconnectAtAll) {
t._tryReconnect();
}
};
sockjs.onopen = function () {
t._onServerOpen();
};
sockjs.onmessage = function (e) {
t._onServerMessage(e.data);
};
sockjs.onclose = function (e) {
t._onServerClose(e);
};
}
return sockjs;
};
DocsCoApi.prototype._onServerOpen = function () {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
this.attemptCount = 0;
}
this._state = ConnectionState.WaitAuth;
this.onFirstConnect();
};
DocsCoApi.prototype._onServerMessage = function (data) {
//TODO: add checks and error handling
//Get data type
var dataObject = JSON.parse(data);
switch (dataObject['type']) {
case 'auth' :
t._onAuth(dataObject);
this._onAuth(dataObject);
break;
case 'message' :
t._onMessages(dataObject, false);
this._onMessages(dataObject, false);
break;
case 'cursor' :
t._onCursor(dataObject);
this._onCursor(dataObject);
break;
case 'meta' :
t._onMeta(dataObject);
this._onMeta(dataObject);
break;
case 'getLock' :
t._onGetLock(dataObject);
this._onGetLock(dataObject);
break;
case 'releaseLock' :
t._onReleaseLock(dataObject);
this._onReleaseLock(dataObject);
break;
case 'connectState' :
t._onConnectionStateChanged(dataObject);
this._onConnectionStateChanged(dataObject);
break;
case 'saveChanges' :
t._onSaveChanges(dataObject);
this._onSaveChanges(dataObject);
break;
case 'saveLock' :
t._onSaveLock(dataObject);
this._onSaveLock(dataObject);
break;
case 'unSaveLock' :
t._onUnSaveLock(dataObject);
this._onUnSaveLock(dataObject);
break;
case 'savePartChanges' :
t._onSavePartChanges(dataObject);
this._onSavePartChanges(dataObject);
break;
case 'drop' :
t._onDrop(dataObject);
this._onDrop(dataObject);
break;
case 'waitAuth' : /*Ждем, когда придет auth, документ залочен*/
break;
case 'error' : /*Старая версия sdk*/
t._onDrop(dataObject);
this._onDrop(dataObject);
break;
case 'documentOpen' :
t._documentOpen(dataObject);
this._documentOpen(dataObject);
break;
case 'warning':
t._onWarning(dataObject);
this._onWarning(dataObject);
break;
case 'license':
t._onLicense(dataObject);
this._onLicense(dataObject);
break;
case 'session' :
t._onSession(dataObject);
this._onSession(dataObject);
break;
case 'refreshToken' :
t._onRefreshToken(dataObject["messages"]);
this._onRefreshToken(dataObject["messages"]);
break;
case 'expiredToken' :
t._onExpiredToken();
this._onExpiredToken();
break;
case 'forceSaveStart' :
t._onForceSaveStart(dataObject["messages"]);
this._onForceSaveStart(dataObject["messages"]);
break;
case 'forceSave' :
t._onForceSave(dataObject["messages"]);
this._onForceSave(dataObject["messages"]);
break;
}
};
DocsCoApi.prototype._onServerClose = function (evt) {
if (ConnectionState.SaveChanges === this._state) {
// Мы сохраняли изменения и разорвалось соединение
this._isReSaveAfterAuth = true;
// Очищаем предыдущий таймер
if (null !== this.saveCallbackErrorTimeOutId) {
clearTimeout(this.saveCallbackErrorTimeOutId);
}
}
this._state = ConnectionState.Reconnect;
var bIsDisconnectAtAll = ((c_oCloseCode.serverShutdown <= evt.code && evt.code <= c_oCloseCode.jwtError) ||
this.attemptCount >= this.maxAttemptCount);
var errorCode = null;
if (bIsDisconnectAtAll) {
this._state = ConnectionState.ClosedAll;
errorCode = this._getDisconnectErrorCode(evt.code);
}
if (this.onDisconnect) {
this.onDisconnect(evt.reason, errorCode);
}
//Try reconect
if (!bIsDisconnectAtAll) {
this._tryReconnect();
}
};
DocsCoApi.prototype._tryReconnect = function() {
var t = this;
if (this.reconnectTimeout) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment