event-source-polyfill 0.0.7 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Gruntfile.js +2 -2
- package/README.md +41 -9
- package/bower.json +2 -2
- package/component.json +3 -3
- package/package.json +2 -2
- package/php/events.php +25 -25
- package/php/index.html +22 -22
- package/src/eventsource.js +673 -0
- package/src/eventsource.min.js +6 -0
- package/tests/bla.html +24 -0
- package/tests/{estests.php → events/index.php} +0 -0
- package/tests/eventsource.js +496 -364
- package/tests/example.html +1 -1
- package/tests/fetch.txt +269 -0
- package/tests/nodechat.js +7 -7
- package/tests/server.js +12 -2
- package/tests/simple/run.bat +2 -0
- package/tests/simple/server.js +33 -0
- package/tests/simple/test.html +24 -0
- package/tests/tests.js +470 -400
- package/eventsource.js +0 -541
- package/eventsource.min.js +0 -6
package/Gruntfile.js
CHANGED
package/README.md
CHANGED
|
@@ -14,13 +14,34 @@ npm install event-source-polyfill
|
|
|
14
14
|
bower install event-source-polyfill
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Just include `eventsource.js` or `eventsource.min.js` in your page to use the polyfill.
|
|
17
|
+
Just include `src/eventsource.js` or `src/eventsource.min.js` in your page to use the polyfill.
|
|
18
18
|
|
|
19
19
|
|
|
20
|
+
Ionic2/Angular2 Installation:
|
|
21
|
+
-----------------------------
|
|
22
|
+
|
|
23
|
+
Unless a typescript definition file is created for this polyfill, this is how you would use it in an Ionic2 project. It should (in theory) be very similar in an Angular2 project.
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
npm install event-source-polyfill
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Add to (or create) src/app/polyfills.ts (path is relative to where polyfills.ts is) :
|
|
30
|
+
```
|
|
31
|
+
import 'path/to/event-source-polyfill/src/eventsource.min.js'
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Add anywhere you need access to EventSourcePolyfill class :
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
declare var EventSourcePolyfill: any;
|
|
38
|
+
```
|
|
39
|
+
|
|
20
40
|
Browser support:
|
|
21
41
|
----------------
|
|
22
42
|
|
|
23
|
-
* IE
|
|
43
|
+
* IE 10+, Firefox 3.5+, Chrome 3+, Safari 4+, Opera 12+
|
|
44
|
+
* IE 8 - IE 9: XDomainRequest is used internally, which has some limitations (2KB padding is requried, no way to send cookies, no way to use client certificates)
|
|
24
45
|
* It works on Mobile Safari, Opera Mobile, Chrome for Android, Firefox for Android
|
|
25
46
|
* It does not work on: Android Browser(requires 4 KB padding), Opera Mini
|
|
26
47
|
|
|
@@ -28,7 +49,7 @@ Advantages:
|
|
|
28
49
|
-----------
|
|
29
50
|
|
|
30
51
|
* Simple server-side code
|
|
31
|
-
* Cross-domain requests support
|
|
52
|
+
* Cross-domain requests support
|
|
32
53
|
|
|
33
54
|
Server-side requirements:
|
|
34
55
|
-------------------------
|
|
@@ -45,12 +66,22 @@ Specification:
|
|
|
45
66
|
Build:
|
|
46
67
|
------
|
|
47
68
|
|
|
48
|
-
* To build EventSource, just install npm modules (`npm install`) and then run the build (`npm run build`). It should generate a new version of eventsource.min.js
|
|
69
|
+
* To build EventSource, just install npm modules (`npm install`) and then run the build (`npm run build`). It should generate a new version of `src/eventsource.min.js`.
|
|
49
70
|
|
|
50
71
|
Notes:
|
|
51
72
|
-----
|
|
52
73
|
* If you are using HTTP Basic Authentication, you can embed credentials into the URL - `http://username:password@github.com`.
|
|
53
74
|
|
|
75
|
+
Custom Headers:
|
|
76
|
+
---------------
|
|
77
|
+
```
|
|
78
|
+
var es = new EventSourcePolyfill('/events', {
|
|
79
|
+
headers: {
|
|
80
|
+
'X-Custom-Header': 'value'
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
54
85
|
Other EventSource polyfills:
|
|
55
86
|
----------------------------
|
|
56
87
|
|
|
@@ -80,11 +111,12 @@ http.createServer(function (request, response) {
|
|
|
80
111
|
|
|
81
112
|
response.writeHead(200, {
|
|
82
113
|
"Content-Type": "text/event-stream",
|
|
83
|
-
"Cache-Control": "no-
|
|
114
|
+
"Cache-Control": "no-store",
|
|
84
115
|
"Access-Control-Allow-Origin": "*"
|
|
85
116
|
});
|
|
86
117
|
|
|
87
|
-
|
|
118
|
+
var padding = new Array(2049);
|
|
119
|
+
response.write(":" + padding.join(" ") + "\n"); // 2kB padding for IE
|
|
88
120
|
response.write("retry: 2000\n");
|
|
89
121
|
|
|
90
122
|
var lastEventId = Number(request.headers["last-event-id"]) || Number(parsedURL.query.lastEventId) || 0;
|
|
@@ -112,7 +144,7 @@ http.createServer(function (request, response) {
|
|
|
112
144
|
if (pathname === "/") {
|
|
113
145
|
pathname = "/index.html";
|
|
114
146
|
}
|
|
115
|
-
if (pathname === "/index.html" || pathname === "../eventsource.js") {
|
|
147
|
+
if (pathname === "/index.html" || pathname === "../src/eventsource.js") {
|
|
116
148
|
response.writeHead(200, {
|
|
117
149
|
"Content-Type": pathname === "/index.html" ? "text/html" : "text/javascript"
|
|
118
150
|
});
|
|
@@ -129,7 +161,7 @@ or use PHP (see php/events.php)
|
|
|
129
161
|
<?php
|
|
130
162
|
|
|
131
163
|
header("Content-Type: text/event-stream");
|
|
132
|
-
header("Cache-Control: no-
|
|
164
|
+
header("Cache-Control: no-store");
|
|
133
165
|
header("Access-Control-Allow-Origin: *");
|
|
134
166
|
|
|
135
167
|
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
|
|
@@ -163,7 +195,7 @@ index.html (php/index.html):
|
|
|
163
195
|
<meta charset="utf-8" />
|
|
164
196
|
<title>EventSource example</title>
|
|
165
197
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
166
|
-
<script src="../eventsource.js"></script>
|
|
198
|
+
<script src="../src/eventsource.js"></script>
|
|
167
199
|
<script>
|
|
168
200
|
var es = new EventSource("events.php");
|
|
169
201
|
var listener = function (event) {
|
package/bower.json
CHANGED
package/component.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "event-source-polyfill",
|
|
3
3
|
"repo": "yaffle/eventsource",
|
|
4
4
|
"description": "a polyfill for http://www.w3.org/TR/eventsource/",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.12",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"sse",
|
|
8
8
|
"server sent events",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"dependencies": {},
|
|
14
14
|
"development": {},
|
|
15
15
|
"license": "MIT",
|
|
16
|
-
"main": "eventsource.js",
|
|
16
|
+
"main": "src/eventsource.js",
|
|
17
17
|
"scripts": [
|
|
18
|
-
"eventsource.js"
|
|
18
|
+
"src/eventsource.js"
|
|
19
19
|
]
|
|
20
20
|
}
|
package/package.json
CHANGED
package/php/events.php
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
<?php
|
|
2
|
-
|
|
3
|
-
header("Content-Type: text/event-stream");
|
|
4
|
-
header("Cache-Control: no-
|
|
5
|
-
header("Access-Control-Allow-Origin: *");
|
|
6
|
-
|
|
7
|
-
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
|
|
8
|
-
if ($lastEventId == 0) {
|
|
9
|
-
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
|
|
13
|
-
echo "retry: 2000\n";
|
|
14
|
-
|
|
15
|
-
// event-stream
|
|
16
|
-
$i = $lastEventId;
|
|
17
|
-
$c = $i + 100;
|
|
18
|
-
while (++$i < $c) {
|
|
19
|
-
echo "id: " . $i . "\n";
|
|
20
|
-
echo "data: " . $i . ";\n\n";
|
|
21
|
-
ob_flush();
|
|
22
|
-
flush();
|
|
23
|
-
sleep(1);
|
|
24
|
-
}
|
|
25
|
-
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
header("Content-Type: text/event-stream");
|
|
4
|
+
header("Cache-Control: no-store");
|
|
5
|
+
header("Access-Control-Allow-Origin: *");
|
|
6
|
+
|
|
7
|
+
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
|
|
8
|
+
if ($lastEventId == 0) {
|
|
9
|
+
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
|
|
13
|
+
echo "retry: 2000\n";
|
|
14
|
+
|
|
15
|
+
// event-stream
|
|
16
|
+
$i = $lastEventId;
|
|
17
|
+
$c = $i + 100;
|
|
18
|
+
while (++$i < $c) {
|
|
19
|
+
echo "id: " . $i . "\n";
|
|
20
|
+
echo "data: " . $i . ";\n\n";
|
|
21
|
+
ob_flush();
|
|
22
|
+
flush();
|
|
23
|
+
sleep(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
26
|
?>
|
package/php/index.html
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8" />
|
|
5
|
-
<title>EventSource example</title>
|
|
6
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
7
|
-
<script src="../eventsource.js"></script>
|
|
8
|
-
<script>
|
|
9
|
-
var es = new EventSource("events.php");
|
|
10
|
-
var listener = function (event) {
|
|
11
|
-
var div = document.createElement("div");
|
|
12
|
-
var type = event.type;
|
|
13
|
-
div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url)));
|
|
14
|
-
document.body.appendChild(div);
|
|
15
|
-
};
|
|
16
|
-
es.addEventListener("open", listener);
|
|
17
|
-
es.addEventListener("message", listener);
|
|
18
|
-
es.addEventListener("error", listener);
|
|
19
|
-
</script>
|
|
20
|
-
</head>
|
|
21
|
-
<body>
|
|
22
|
-
</body>
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<title>EventSource example</title>
|
|
6
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
7
|
+
<script src="../src/eventsource.js"></script>
|
|
8
|
+
<script>
|
|
9
|
+
var es = new EventSource("events.php");
|
|
10
|
+
var listener = function (event) {
|
|
11
|
+
var div = document.createElement("div");
|
|
12
|
+
var type = event.type;
|
|
13
|
+
div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url)));
|
|
14
|
+
document.body.appendChild(div);
|
|
15
|
+
};
|
|
16
|
+
es.addEventListener("open", listener);
|
|
17
|
+
es.addEventListener("message", listener);
|
|
18
|
+
es.addEventListener("error", listener);
|
|
19
|
+
</script>
|
|
20
|
+
</head>
|
|
21
|
+
<body>
|
|
22
|
+
</body>
|
|
23
23
|
</html>
|