Commit 5047b664 authored by Alexander.Trofimov's avatar Alexander.Trofimov Committed by Alexander.Trofimov

Поправил sockJS

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@66450 954022d7-b5bf-4e40-9824-e11837661b57
parent 24fc325b
"use strict"; "use strict";
(function (global) { (function(global) {
'use strict'; 'use strict';
// Класс надстройка, для online и offline работы // Класс надстройка, для online и offline работы
var CSpellCheckApi = function (options) { var CSpellCheckApi = function(options) {
this._SpellCheckApi = new SpellCheckApi(); this._SpellCheckApi = new SpellCheckApi();
this._onlineWork = false; this._onlineWork = false;
...@@ -14,44 +14,51 @@ ...@@ -14,44 +14,51 @@
} }
}; };
CSpellCheckApi.prototype.init = function (docid) { CSpellCheckApi.prototype.init = function(docid) {
if (this._SpellCheckApi && this._SpellCheckApi.isRightURL()) { if (this._SpellCheckApi && this._SpellCheckApi.isRightURL()) {
var t = this; var t = this;
this._SpellCheckApi.onDisconnect = function (e, isDisconnectAtAll, isCloseCoAuthoring) {t.callback_OnDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);}; this._SpellCheckApi.onDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
this._SpellCheckApi.onSpellCheck = function (e) {t.callback_OnSpellCheck(e);}; t.callback_OnDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
};
this._SpellCheckApi.onSpellCheck = function(e) {
t.callback_OnSpellCheck(e);
};
this._SpellCheckApi.init(docid); this._SpellCheckApi.init(docid);
this._onlineWork = true; this._onlineWork = true;
} }
}; };
CSpellCheckApi.prototype.set_url = function (url) { CSpellCheckApi.prototype.set_url = function(url) {
if (this._SpellCheckApi) if (this._SpellCheckApi) {
this._SpellCheckApi.set_url(url); this._SpellCheckApi.set_url(url);
}
}; };
CSpellCheckApi.prototype.get_state = function () { CSpellCheckApi.prototype.get_state = function() {
if (this._SpellCheckApi) if (this._SpellCheckApi) {
return this._SpellCheckApi.get_state(); return this._SpellCheckApi.get_state();
}
return 0; return 0;
}; };
CSpellCheckApi.prototype.disconnect = function () { CSpellCheckApi.prototype.disconnect = function() {
if (this._SpellCheckApi && this._onlineWork) { if (this._SpellCheckApi && this._onlineWork) {
this._SpellCheckApi.disconnect(); this._SpellCheckApi.disconnect();
} }
}; };
CSpellCheckApi.prototype.spellCheck = function (spellCheckData) { CSpellCheckApi.prototype.spellCheck = function(spellCheckData) {
if (this._SpellCheckApi && this._onlineWork) { if (this._SpellCheckApi && this._onlineWork) {
this._SpellCheckApi.spellCheck(spellCheckData); this._SpellCheckApi.spellCheck(spellCheckData);
} }
}; };
CSpellCheckApi.prototype.callback_OnSpellCheck = function (e) { CSpellCheckApi.prototype.callback_OnSpellCheck = function(e) {
if (this.onSpellCheck) if (this.onSpellCheck) {
return this.onSpellCheck (e); return this.onSpellCheck(e);
}
}; };
/** /**
...@@ -59,9 +66,10 @@ ...@@ -59,9 +66,10 @@
* @param {jQuery} e event об отсоединении с причиной * @param {jQuery} e event об отсоединении с причиной
* @param {Bool} isDisconnectAtAll окончательно ли отсоединяемся(true) или будем пробовать сделать reconnect(false) + сами отключились * @param {Bool} isDisconnectAtAll окончательно ли отсоединяемся(true) или будем пробовать сделать reconnect(false) + сами отключились
*/ */
CSpellCheckApi.prototype.callback_OnDisconnect = function (e, isDisconnectAtAll, isCloseCoAuthoring) { CSpellCheckApi.prototype.callback_OnDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
if (this.onDisconnect) if (this.onDisconnect) {
return this.onDisconnect (e, isDisconnectAtAll, isCloseCoAuthoring); return this.onDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
}
}; };
/** States /** States
...@@ -70,7 +78,7 @@ ...@@ -70,7 +78,7 @@
* 1 - opened * 1 - opened
* 3 - closed * 3 - closed
*/ */
var SpellCheckApi = function (options) { var SpellCheckApi = function(options) {
if (options) { if (options) {
this.onDisconnect = options.onDisconnect; this.onDisconnect = options.onDisconnect;
this.onConnect = options.onConnect; this.onConnect = options.onConnect;
...@@ -86,29 +94,29 @@ ...@@ -86,29 +94,29 @@
this._url = ""; this._url = "";
}; };
SpellCheckApi.prototype.isRightURL = function () { SpellCheckApi.prototype.isRightURL = function() {
return ("" != this._url); return ("" != this._url);
}; };
SpellCheckApi.prototype.set_url = function (url) { SpellCheckApi.prototype.set_url = function(url) {
this._url = url; this._url = url;
}; };
SpellCheckApi.prototype.get_state = function () { SpellCheckApi.prototype.get_state = function() {
return this._state; return this._state;
}; };
SpellCheckApi.prototype.spellCheck = function (spellCheckData) { SpellCheckApi.prototype.spellCheck = function(spellCheckData) {
this._send({"type": "spellCheck", "spellCheckData": spellCheckData}); this._send({"type": "spellCheck", "spellCheckData": spellCheckData});
}; };
SpellCheckApi.prototype.disconnect = function () { SpellCheckApi.prototype.disconnect = function() {
// Отключаемся сами // Отключаемся сами
this.isCloseCoAuthoring = true; this.isCloseCoAuthoring = true;
return this.sockjs.close(); return this.sockjs.close();
}; };
SpellCheckApi.prototype._send = function (data) { SpellCheckApi.prototype._send = function(data) {
if (data !== null && typeof data === "object") { if (data !== null && typeof data === "object") {
if (this._state > 0) { if (this._state > 0) {
this.sockjs.send(JSON.stringify(data)); this.sockjs.send(JSON.stringify(data));
...@@ -118,23 +126,24 @@ ...@@ -118,23 +126,24 @@
} }
}; };
SpellCheckApi.prototype._sendAfterConnect = function () { SpellCheckApi.prototype._sendAfterConnect = function() {
var data; var data;
while (this._state > 0 && undefined !== (data = this.dataNeedSend.shift())) while (this._state > 0 && undefined !== (data = this.dataNeedSend.shift()))
this._send(data); this._send(data);
}; };
SpellCheckApi.prototype._onSpellCheck = function (data) { SpellCheckApi.prototype._onSpellCheck = function(data) {
if (undefined !== data["spellCheckData"] && this.onSpellCheck) if (undefined !== data["spellCheckData"] && this.onSpellCheck) {
this.onSpellCheck(data["spellCheckData"]); this.onSpellCheck(data["spellCheckData"]);
}
}; };
var reconnectTimeout, attemptCount=0; var reconnectTimeout, attemptCount = 0;
function initSocksJs(url, docsCoApi) { function initSocksJs(url, docsCoApi) {
var sockjs = new SockJS(url, null, {debug: true}); var sockjs = new (_getSockJs())(url, null, {debug: true});
sockjs.onopen = function () { sockjs.onopen = function() {
if (reconnectTimeout) { if (reconnectTimeout) {
clearTimeout(reconnectTimeout); clearTimeout(reconnectTimeout);
attemptCount = 0; attemptCount = 0;
...@@ -148,25 +157,29 @@ ...@@ -148,25 +157,29 @@
docsCoApi._sendAfterConnect(); docsCoApi._sendAfterConnect();
}; };
sockjs.onmessage = function (e) { sockjs.onmessage = function(e) {
//TODO: add checks and error handling //TODO: add checks and error handling
//Get data type //Get data type
var dataObject = JSON.parse(e.data); var dataObject = JSON.parse(e.data);
var type = dataObject.type; var type = dataObject.type;
switch (type) { switch (type) {
case 'spellCheck' : docsCoApi._onSpellCheck(dataObject); break; case 'spellCheck' :
docsCoApi._onSpellCheck(dataObject);
break;
} }
}; };
sockjs.onclose = function (evt) { sockjs.onclose = function(evt) {
docsCoApi._state = -1; // Reconnect state docsCoApi._state = -1; // Reconnect state
var bIsDisconnectAtAll = attemptCount >= 20 || docsCoApi.isCloseCoAuthoring; var bIsDisconnectAtAll = attemptCount >= 20 || docsCoApi.isCloseCoAuthoring;
if (bIsDisconnectAtAll) if (bIsDisconnectAtAll) {
docsCoApi._state = 3; // Closed state docsCoApi._state = 3;
} // Closed state
if (docsCoApi.onDisconnect) { if (docsCoApi.onDisconnect) {
docsCoApi.onDisconnect(evt.reason, bIsDisconnectAtAll, docsCoApi.isCloseCoAuthoring); docsCoApi.onDisconnect(evt.reason, bIsDisconnectAtAll, docsCoApi.isCloseCoAuthoring);
} }
if (docsCoApi.isCloseCoAuthoring) if (docsCoApi.isCloseCoAuthoring) {
return; return;
}
//Try reconect //Try reconect
if (attemptCount < 20) { if (attemptCount < 20) {
tryReconnect(); tryReconnect();
...@@ -174,25 +187,28 @@ ...@@ -174,25 +187,28 @@
}; };
function tryReconnect() { function tryReconnect() {
if (reconnectTimeout) if (reconnectTimeout) {
{
clearTimeout(reconnectTimeout); clearTimeout(reconnectTimeout);
} }
attemptCount++; attemptCount++;
reconnectTimeout = setTimeout(function(){ reconnectTimeout = setTimeout(function() {
delete docsCoApi.sockjs; delete docsCoApi.sockjs;
docsCoApi.sockjs = initSocksJs(url,docsCoApi); docsCoApi.sockjs = initSocksJs(url, docsCoApi);
},500*attemptCount); }, 500 * attemptCount);
} }
return sockjs; return sockjs;
} }
SpellCheckApi.prototype.init = function (docid) { function _getSockJs() {
return window['SockJS'] ? window['SockJS'] : require('sockjs');
}
SpellCheckApi.prototype.init = function(docid) {
this._docid = docid; this._docid = docid;
//Begin send auth //Begin send auth
this.sockjs_url = this._url + '/doc/'+docid+'/c'; this.sockjs_url = this._url + '/doc/' + docid + '/c';
this.sockjs = initSocksJs(this.sockjs_url, this); this.sockjs = initSocksJs(this.sockjs_url, this);
}; };
global["CSpellCheckApi"] = CSpellCheckApi; global["CSpellCheckApi"] = CSpellCheckApi;
......
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