Skip to content

Commit

Permalink
syntax highlighting for readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaffle committed Sep 28, 2011
1 parent be90cfd commit 45f6f83
Showing 1 changed file with 102 additions and 98 deletions.
200 changes: 102 additions & 98 deletions README.md
Expand Up @@ -49,107 +49,111 @@ EXAMPLE
server-side (node.js)
---------------------

var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
var t = null;
if (req.url.indexOf('/events') === 0) {

res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
});

res.write(':' + Array(2049).join(' ') + '\n'); //2kb padding for IE
res.write('data: ' + Date() + '\n\n');

t = setInterval(function () {
res.write('data: ' + Date() + '\n\n');
}, 1000);

res.socket.on('close', function () {
clearInterval(t);
});

if (req.headers.polling) {
res.end();
}

} else {
if (req.url === '/index.html' || req.url === '/eventsource.js') {
res.writeHead(200, {'Content-Type': req.url === '/index.html' ? 'text/html' : 'text/javascript'});
res.write(fs.readFileSync(__dirname + req.url));
}
res.end();
}
}).listen(8081); //! port :8081
```javascript
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
var t = null;
if (req.url.indexOf('/events') === 0) {

res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
});

res.write(':' + Array(2049).join(' ') + '\n'); //2kb padding for IE
res.write('data: ' + Date() + '\n\n');

t = setInterval(function () {
res.write('data: ' + Date() + '\n\n');
}, 1000);

res.socket.on('close', function () {
clearInterval(t);
});

if (req.headers.polling) {
res.end();
}

} else {
if (req.url === '/index.html' || req.url === '/eventsource.js') {
res.writeHead(200, {'Content-Type': req.url === '/index.html' ? 'text/html' : 'text/javascript'});
res.write(fs.readFileSync(__dirname + req.url));
}
res.end();
}
}).listen(8081); //! port :8081
```

or use PHP (see php/events.php)
-------------------------------

<?

header('Content-Type: text/event-stream');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');

// prevent bufferring
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

// getting last-event-id from POST (for IE) and from Headers
if (preg_match('#Last\\-Event\\-ID\\=([\\s\\S]+)#ui', @$HTTP_RAW_POST_DATA, $tmp)) {
$lastEventId = urldecode(@$tmp[1]);
} else {
$headers = getallheaders();
$lastEventId = @$headers['Last-Event-ID'];
}

// 2kb padding for IE
echo ':' . str_repeat(' ', 2048) . "\n";

// event-stream
for ($i = intval($lastEventId) + 1; $i < 100; $i++) {
echo "id: $i\n";
echo "data: $i;\n\n";
sleep(1);
}

?>
```php
<?

header('Content-Type: text/event-stream');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');

// prevent bufferring
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

// getting last-event-id from POST (for IE) and from Headers
if (preg_match('#Last\\-Event\\-ID\\=([\\s\\S]+)#ui', @$HTTP_RAW_POST_DATA, $tmp)) {
$lastEventId = urldecode(@$tmp[1]);
} else {
$headers = getallheaders();
$lastEventId = @$headers['Last-Event-ID'];
}

// 2kb padding for IE
echo ':' . str_repeat(' ', 2048) . "\n";

// event-stream
for ($i = intval($lastEventId) + 1; $i < 100; $i++) {
echo "id: $i\n";
echo "data: $i;\n\n";
sleep(1);
}

?>
```

index.html (php/index.html):
----------------------------

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>EventSource example</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="../eventsource.js"></script>
<script>
var es = new EventSource('events.php');
es.addEventListener('open', function (event) {
var div = document.createElement('div');
div.innerHTML = 'opened: ' + es.url;
document.body.appendChild(div);
}, false);
es.addEventListener('message', function (event) {
document.body.appendChild(document.createTextNode(event.data));
}, false);
es.addEventListener('error', function (event) {
var div = document.createElement('div');
div.innerHTML = 'closed';
document.body.appendChild(div);
}, false);
</script>
</head>
<body>
</body>
</html>
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>EventSource example</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="../eventsource.js"></script>
<script>
var es = new EventSource('events.php');
es.addEventListener('open', function (event) {
var div = document.createElement('div');
div.innerHTML = 'opened: ' + es.url;
document.body.appendChild(div);
}, false);
es.addEventListener('message', function (event) {
document.body.appendChild(document.createTextNode(event.data));
}, false);
es.addEventListener('error', function (event) {
var div = document.createElement('div');
div.innerHTML = 'closed';
document.body.appendChild(div);
}, false);
</script>
</head>
<body>
</body>
</html>
```

0 comments on commit 45f6f83

Please sign in to comment.