Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$ nodester app info sse-chat
nodester info Gathering information about: sse-chat
nodester info sse-chat on port 10299 running: true (pid: 18353)$ nodester app restart sse-chat
nodester info Attemping to restart app: sse-chat
nodester info app restarted.$ nodester app logs sse-chat
nodester info Showing logs for: sse-chat
Munging require paths..
Globallizing Buffer
Reading file...
Nodester wrapped script starting (18369) at Thu, 02 Jun 2011 09:39:33 GMT
[INFO] Nodester listening on port: 10299
Online: Thu Jun 02 2011 09:39:33 GMT+0000 (UTC)Для того, чтобы у некоторых браузеров не было бесконечной загрузки мы вместо $.ready выполняем setTimeout:
setTimeout(function () { // Ставлю именно таймаут, а не $.ready иначе у вебкитов будет бесконечная загрузка // код }, 50);
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
@Chat: connection error
Maxim: Hi
@ChatBot: Maxim online
@ChatBot: Hello, Maxim
Welcome to EventSource Chat!//JS side of dirt SSE
//timeout to load indicator skip
setTimeout(function(){
var transport = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
transport.open('GET', 'test.php', true);
transport.onreadystatechange=parse;
transport.send();
},500);
function parse(){
//make element with id = response inside html or use console.log
document.getElementById('response').innerHTML = this.responseText;
}
<?php
//php side of SSE
set_time_limit(0);
header("Content-type: text/event-stream");
while(true){
echo time().'
';
//ob_flush and flush is needed, without it not be output
ob_flush();
flush();
sleep(1);
}
?>
Content-type: стихи/пушкина), кроме появления свойства responseXML (Content-type: text/xml). Функция parse же вызывается несколько раз из-за того, что проходят куски данных (php функция flush) и срабатывает переключение на readyState 3 несколько раз.var request = new XMLHttpRequest()
request.open('GET', 'http://javascript.ru/server_push/long_digits.php?r=0.8904660024932128', true);
request.onreadystatechange = function(){
console.log('readyState: %d, responseText: %s',request.readyState, request.responseText);
};
request.send();readyState: 1, responseText:
readyState: 2, responseText:
readyState: 3, responseText: 1 2
readyState: 3, responseText: 1 2 3
readyState: 3, responseText: 1 2 3 4
readyState: 3, responseText: 1 2 3 4 5
readyState: 3, responseText: 1 2 3 4 5 6
readyState: 3, responseText: 1 2 3 4 5 6 7
readyState: 3, responseText: 1 2 3 4 5 6 7 8
readyState: 3, responseText: 1 2 3 4 5 6 7 8 9
readyState: 3, responseText: 1 2 3 4 5 6 7 8 9 10
readyState: 4, responseText: 1 2 3 4 5 6 7 8 9 10
Создание приложений реального времени с помощью Server-Sent Events