相关疑难解决方法(0)

WebSockets与服务器发送的事件/事件源

双方的WebSockets和服务器发送的事件能够将数据推送到浏览器.对我来说,他们似乎是竞争技术.他们之间有什么区别?你何时会选择一个而不是另一个?

html5 websocket server-sent-events

780
推荐指数
7
解决办法
19万
查看次数

HTML5 Server-Sent Events原型设计 - 模糊错误和重复轮询?

我想它们符合我的要求完美,看起来他们应该是简单的实现获得与服务器端事件交手,但我不能让过去一个模糊的错误,什么样子反复被关闭了连接并重新-opened.我尝试的一切都基于这个和其他教程.

PHP是一个单独的脚本:

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

function sendMsg($id, $msg) {
  echo "id: $id" . PHP_EOL;
  echo "data: $msg" . PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}

$serverTime = time();
sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
?>
Run Code Online (Sandbox Code Playgroud)

并且JavaScript看起来像这样(在身体负载上运行):

function init() {

    var source;
    if (!!window.EventSource) {
        source = new EventSource('events.php');
        source.addEventListener('message', function(e) {
            document.getElementById('output').innerHTML += e.data + '<br />';
        }, false);
        source.addEventListener('open', function(e) {
            document.getElementById('output').innerHTML += 'connection opened<br />';
        }, false);
        source.addEventListener('error', function(e) {
            document.getElementById('output').innerHTML += 'error<br />'; …
Run Code Online (Sandbox Code Playgroud)

apache html5 server-push server-sent-events

13
推荐指数
1
解决办法
1万
查看次数