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>

app /アクション/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と、5秒ごとにエラーなしで時間が表示されます。

http://localhost:3000/timeただし、PHPコードでは、URIをstream.phpinに置き換えても問題なく機能します。sse_time.html

stream.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

ケーススタディを参照してください:STREAMCONGRESSのリアルタイム更新

EventSourceで覚えておくべき重要な問題は、クロスドメイン接続が許可されていないことです。つまり、Crampアプリは、メインのRailsアプリと同じstreamcongress.comドメインから提供する必要があります。

私は、htmlページもクランプアプリケーションの一部である必要があることを理解しています(代替手段はありますが)。だから私は次の変更を加えて、それはうまくいきました。

Redis Pub / Sub+WebSocketsを使用したクランプチャットapp/actions/home_action.rbに基づいて変更

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 に答える