forked from byjg/jquery-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.sse.js
200 lines (167 loc) · 6.58 KB
/
jquery.sse.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
/*
* jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill v0.1.3
* https://github.com/byjg/jquery-sse
*
* This document is licensed as free software under the terms of the
* MIT License: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2015 by JG (João Gilberto Magalhães).
*/
(function ($) {
$.extend({
SSE: function (url, customSettings) {
var sse = {instance: null, type: null};
var settings = {
onOpen: function (e) {
},
onEnd: function (e) {
},
onError: function (e) {
},
onMessage: function (e) {
},
options: {},
headers: {},
events: {}
};
$.extend(settings, customSettings);
sse._url = url;
sse._settings = settings;
// Start the proper EventSource object or Ajax fallback
sse._start = sse.start;
sse.start = function () {
if (this.instance) {
return false;
}
if (!window.EventSource || this._settings.options.forceAjax || (Object.keys(this._settings.headers).length > 0)) {
createAjax(this);
} else {
createEventSource(this);
}
return true;
};
// Stop the proper object
sse.stop = function () {
if (!this.instance) {
return false;
}
if (!window.EventSource || this._settings.options.forceAjax || (Object.keys(this._settings.headers).length > 0)) {
// Nothing to do;
} else {
this.instance.close();
}
this._settings.onEnd();
this.instance = null;
this.type = null;
return true;
};
return sse;
}
});
// Private Method for Handle EventSource object
function createEventSource(me) {
me.type = 'event';
me.instance = new EventSource(me._url);
me.instance.successCount = 0;
me.instance.onmessage = me._settings.onMessage;
me.instance.onopen = function (e) {
if (me.instance.successCount++ === 0) {
me._settings.onOpen(e);
}
};
me.instance.onerror = function (e) {
if (event.target.readyState === EventSource.CLOSED) {
me._settings.onError(event);
}
};
for (var key in me._settings.events) {
me.instance.addEventListener(key, me._settings.events[key], false);
}
}
// Handle the Ajax instance (fallback)
function createAjax(me) {
me.type = 'ajax';
me.instance = {successCount: 0, id: null, retry: 3000, data: "", event: ""};
runAjax(me);
}
// Handle the continous Ajax request (fallback)
function runAjax(me) {
if (!me.instance) {
return;
}
var headers = {'Last-Event-ID': me.instance.id};
$.extend(headers, me._settings.headers);
$.ajax({
url: me._url,
method: 'GET',
headers: headers,
success: function (receivedData, status, info) {
if (!me.instance) {
return;
}
if (me.instance.successCount++ === 0) {
me._settings.onOpen();
}
var lines = receivedData.split("\n");
// Process the return to generate a compatible SSE response
me.instance.data = "";
var countBreakLine = 0;
for (var key in lines) {
var separatorPos = lines[key].indexOf(":");
var item = [
lines[key].substr(0, separatorPos),
lines[key].substr(separatorPos + 1)
];
switch (item[0]) {
// If the first part is empty, needed to check another sequence
case "":
if (!item[1] && countBreakLine++ === 1) { // Avoid comments!
eventMessage = {
data: me.instance.data,
lastEventId: me.instance.id,
origin: 'http://' + info.getResponseHeader('Host'),
returnValue: true
};
// If there are a custom event then call it
if (me.instance.event && me._settings.events[me.instance.event]) {
me._settings.events[me.instance.event](eventMessage);
} else {
me._settings.onMessage(eventMessage);
}
me.instance.data = "";
me.instance.event = "";
countBreakLine = 0;
}
break;
// Define the new retry object;
case "retry":
countBreakLine = 0;
me.instance.retry = parseInt(item[1].trim());
break;
// Define the new ID
case "id":
countBreakLine = 0;
me.instance.id = item[1].trim();
break;
// Define a custom event
case "event":
countBreakLine = 0;
me.instance.event = item[1].trim();
break;
// Define the data to be processed.
case "data":
countBreakLine = 0;
me.instance.data += (me.instance.data !== "" ? "\n" : "") + item[1].trim();
break;
default:
countBreakLine = 0;
}
}
setTimeout(function () {
runAjax(me);
}, me.instance.retry);
},
error: me._settings.onError
});
}
})(jQuery);