1

我准备了一个示例应用程序,以使用Cramp(Ruby)SSE(HTML5)从服务器实时获取更新。

通过访问 html 时出现以下错误http://localhost/sse_time.html

Errors:
Chrome:
  Uncaught Error: SECURITY_ERR: DOM Exception 18 sse_time.html:9
  Uncaught TypeError: Cannot read property 'key-preview' of undefined 

Firefox:
  Error: The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.
Line: 0

  Error: The connection to http://localhost:3000/time was interrupted while the page was loading.
  Line: 9

sse_time.html

<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>
<script>
  if (!!window.EventSource) {

    var source = new EventSource('http://localhost:3000/time');

    source.addEventListener('message', function(e) {
      console.log(e.data);
    }, false);

    source.addEventListener('open', function(e) {
      // Connection was opened.
      console.log('Connection was opened.');
    }, false);

    source.addEventListener('error', function(e) {
      if (e.readyState == EventSource.CLOSED) {
        // Connection was closed.
        console.log('Connection was closed.');
      }
    }, false);

  } else {
    document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
  }
</script>

</body>
</html>

应用程序/动作/time_action.rb

class TimeAction < Cramp::Action
  self.transport = :sse

  periodic_timer :send_latest_time, every: 5

  def send_latest_time
    render "Current time : #{Time.now}"
  end
end

line 9在哪里var source = new EventSource('http://localhost:3000/time');

如果我http://localhost:3000/time在 chrome 中点击,它会在每 5 秒后显示时间,没有任何错误。

但是使用 PHP 代码可以很好地http://localhost:3000/timestream.phpin替换 URIsse_time.html

流.php

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

/**
 * Constructs the SSE data format and flushes that data to the client.
 *
 * @param string $id Timestamp/id of this connection.
 * @param string $msg Line of text that should be transmitted.
 */
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()));

这里stream.phpsse_time.html居住在同一地点。

有人可以指导我解决这个问题吗?

4

1 回答 1

1

参考案例研究:STREAM CONGRESS 中的实时更新

使用 EventSource 需要牢记的一个重要问题是不允许跨域连接。这意味着必须从与主 Rails 应用程序相同的 streamcongress.com 域提供 Cramp 应用程序。

我意识到 html 页面也需要成为 cramp 应用程序的一部分(尽管有替代方案)。因此,我进行了以下更改,并且成功了。

app/actions/home_action.rb基于使用 Redis Pub/Sub + WebSockets 的 Cramp 聊天修改

class HomeAction < Cramp::Action
  @@template = ERB.new(File.read(PocRailsCramp::Application.root('app/views/index.html.erb')))

  def start
    render @@template.result(binding)
    finish
  end
end

的内容与问题本身app/views/index.html.erb的内容相同。sse_time.html

现在点击http://localhost:3000开始在浏览器控制台上每 5 秒显示一次服务器时间。

于 2012-10-25T12:19:14.073 回答