-
Notifications
You must be signed in to change notification settings - Fork 0
/
sasha.js
404 lines (335 loc) · 11.9 KB
/
sasha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**********************************************************************************************
* @Author: Frank Galos.
* @Email-address: [email protected].
* @date-created: 26/3/2017.
* @copyrights: sasha.
* @website: nevaa.com //when it's created this domain could not be created so it might be different.
*
**********************************************************************************************/
/**********************************************************************************************
* ********************************************************************************************
* ********************************** ***********************************************
* ********************************** SASHA ***********************************************
* ********************************** ***********************************************
* ********************************************************************************************
**********************************************************************************************/
/******************************************************************************************
* *************************************** *************************************
***************************************** COPYRIGHTS *************************************
* *************************************** *************************************
* ****************************************************************************************
*
* This library and it's methods are here by the use of this software. Therefore no one is,
* Allowed to owen or have a piece of this software. whomever found with piece of this,
* Software or it's core class. Strong action will be taken.
*
********************************************************************************************/
/**********************************************************************************************
* ************************ **********************
************************** SEVER SENT EVENT AND LONG POLLING TECHNIQUES **********************
*************************** **********************
* ********************************************************************************************
* This class is for real time data accessing that involve the sse as sever sent event and ajax.
* both techniques they are doing the same thing the only different is sse that is server persistent,
* and ajax -> Long poll is not server persistent
*
**********************************************************************************************/
// initializing the library
var isConnected = 0,
interval = 1000,
connetion = null,
sasha,url = null;
// proccessor
function sasha() {};
// DOM<element> id getter
sasha.prototype.getID = function (id)
{
return document.getElementById(id);
}
// browser support checking
// this is to insure all browser are getting the same information at the exactly moment.
sasha.prototype.supported = function ()
{
var type = "";
if(typeof(EventSource) !== "undefined"){
type = "sse";
}else if (window.XMLHttpRequest){
type = "xhr";
}else if(window.ActiveXObject){
type = "xhr";
}
return type;
}
// make a connection to the server
sasha.prototype.onConnect = function (url)
{
// we're only supporting either sse or xhr
if (this.supported() === "sse" || this.supported() === "xhr"){
if(this.supported() === "sse"){
var sse = new EventSource(url);
var error = this.onError(sse);
// making connection to sse and return request
if(!error){
type = "sse";
return {con:sse,type:type};
}
}else{
// making connection to xhr and return request
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return {con:xhr,type:"xhr"};
}
}else{
return this.onResponse("Unsupported browser","Sorry your browser is not supported by this library","OK");
}
}
// get error information
sasha.prototype.onError = function (e)
{
if (e.readyState === 1){
e.onerror = function () {
sasha.onResponse("Error","Connection failed. Please try again","OK");
}
}
return false;
}
/** open the connection when it's made
* @meth is xhr method either get/pos
* @url is actual url
* @data is query string data that are appended in url for sse.
* @data for xhr are either get or post form data can be represented by forward slash(/) or & and = sign e.g action=value&data=value. the same in htaccess /value/value
*/
sasha.prototype.onOpen = function (objs)
{
var o = objs,
meth = o.meth,
urls = o.url,
data = o.query,
state,obj,con,type,
dataString = data;
if(this.supported() === "sse"){
obj = this.onConnect(urls+"?"+dataString);
con = obj.con;
type = obj.type,
connetion = con,
url = urls;
}else{
if (meth === "GET" || meth === "get"){
obj = this.onConnect(urls+"?"+dataString);
con = obj.con;
type = obj.type,
connetion = con,
url = urls;
}else{
obj = this.onConnect(urls+"");
con = obj.con;
type = obj.type,
connetion = con,
url = urls;
}
}
state = {con:false,status:isConnected,type:null};
if (type === "sse"){
con.onopen = function () {return true;}
if (con){
isConnected = 1;
state = {con:con,status:isConnected,type:type};
}
}else if(type === "xhr"){
con.open(meth,url);
con.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
if(con.readyState === 1){
isConnected = 1;
state = {con:con,status:isConnected,type:type};
}
}
return state;
}
// close the connection if there is any connection exist
sasha.prototype.onClose = function ()
{
if(connetion !== null && this.supported() === "sse"){
if(typeof connetion.close === 'function'){
connetion.close();
}
//console.log("connection to the server [ "+url+" ] closed");
return true;
}else if(connetion !== null && this.supported() === "xhr"){
if(typeof connetion.abort === 'function'){
connetion.abort();
}
//console.log("connection to the server [ "+url+" ] closed");
return true;
}else{
//console.log("connection to the server [ "+url+" ] failed to be closed");
return false;
}
}
/** get server response
* check if there is a conection to the server
* @meth is xhr method either get/pos
* @url is actual url
* @query is query string data that are appended in url for sse,
* And for xhr are either get or post form data can be represented by forward slash(/) or & and = sign e.g action=value&data=value. the same in htaccess /value/value.
* @param is string parameter that represent id of an html element
*/
sasha.prototype.onMessage = function (objs)
{
var o = objs,
meth = o.meth,
url = o.url,
query = o.query,
interval = o.interval,
callback = o.success;
// if there is any connection just close it so that the new connection can be established
if(typeof this.onClose == "function"){
(this.onClose());
}
// get connection components
if(typeof this.onOpen === "function")
var obj = this.onOpen({meth:meth,url:url,query:query}),
con = obj.con,
isConnected = obj.status,
type = obj.type;
// checking if there is connection to this channel
if(isConnected === 1){
if (type === "sse"){
connetion.onmessage = callback;
}else if(type === "xhr"){
con.onreadystatechange = callback;
setTimeout(function(){
sasha.onMessage(objs);
},interval);
sasha.send(con,query,meth);
}
}else{
console.log("Connection Failed");
}
}
sasha.prototype.data = function(obj){
if(typeof obj != 'undefined'){
if(typeof obj.type != 'undefined' && typeof obj.data != 'undefined' && obj.type === 'json'){
return JSON.parse(obj.data.data);
}else{
if(typeof obj.data != 'undefined'){
return obj.data;
}else{
throw console.warn("Error::: data is require");
}
}
}else{
throw console.warn("Error::: object is not defined");
}
}
// show the response to the client
sasha.prototype.onResponse = function (h,b,f)
{
cAlert.renderAlertData(h,b,f);
}
/**
* @param url this caries the informations about the server.
* @param meth is either get/post
*/
sasha.prototype.connect = function (meth,url)
{
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
xhr = new ActiveXObject();
}else {
throw new Error ("Ajax is not supported in your browser");
}
xhr.open(meth,url);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
return xhr;
}
sasha.prototype.cancel = function(arg){
var c = arg.cancelable = true;
if(c){
console.log("Conection cancel");
}else{
console.log("Failed to cancel the connection");
}
return c;
}
/**
* @param con this caries the connection informations.
* Here i checked for connection state and status.
*/
sasha.prototype.state = function (con)
{
if (con.readyState === 4){
if(con.status === 200 && con.status < 300){
return true;
}
}
return false;
}
/**
* @param url this caries the informations about the server.
* @param query this caries information that is about to sent to the server.
* @param meth this caries information that is about to checked. this method are either POST/GET
*/
sasha.prototype.response = function (obj)
{
var con,
o = obj,
meth = o.meth,
url = o.url,
query = o.query,
callback = o.success;
if(meth === "get" || meth === "GET"){
con = this.connect(meth,url+"/"+query);
}else if(meth === "POST" || meth === "post"){
con = this.connect(meth,url);
}
con.onreadystatechange = callback;
sasha.send(con,query,meth);
}
sasha.prototype.ready = function (target) {
if(target.readyState === 4){
if(target.status === 200 && target.status < 300){
return {boolen:true,type:'xhr'};
}
}else if(target.readyState === 1){
return {boolen:true,type:'sse'};
}
return {boolen:false,type:null};
};
sasha.prototype.onData = function(target){
var r = this.ready(target.target),x = '';
if(r.boolen && r.type === 'xhr'){
x = target.target.responseText;
}else if(r.boolen && r.type === 'sse'){
x = target.event.data;
}
if(r.boolen){
return {response:x};
}
};
/**
* @param con this caries the connection informations that returned by connect method.
* @param query this caries information that is about to sent to the server.
* @param method this caries information that is about to checked. this method are either POST/GET
*/
sasha.prototype.send = function (con,query,method)
{
if (method === "GET" || method === "get"){
con.send(null);
}else{
con.send(query);
}
};
/** library call
* @type {sasha}
* Test url: 'http://localhost/route/chat.live/'
* Test query: 'action=getMessages&sender=frank&receiver=galos'
* Test param: 'ms-body' this is message body,for displaying all messages
*/
sasha = new sasha();