3

そのため、メッセージをリアルタイムでブラウザに送信する必要があるアプリケーションを実装しています。現在、これは正常に機能しています。メッセージを受信すると、時計の例に似た Untyped Actor になります。

私の問題は、コメットソケットが切断されたときにWebページに再接続できるようにしたいということです。現在、comet ソケットを備えたクロムでは、ロード中のアイコンが継続的に回転します。iframe/comet ソケットの切断メッセージをキャッチする方法はありますか? または、javascript/jquery でポーリングできるものはありますか? それで、ページをリロードできますか?

4

1 に答える 1

0

「ウェブページ」に再接続したい場合 (つまり、ブラウザーがサーバーに別のリクエストを送信するようにする場合、window.location.reload()または他の方法を使用する場合)、標準のplay.libs.Comet.onDisconnectedハンドラーは役に立ちません - そのドメインはクライアント側ではなくサーバー側。

クライアント側で考えられる停電を独自に管理するには、「ハートビート」スキームを実装する必要がある場合があります。クライアント アプリケーションは、前のメッセージから時間が経過しすぎると、サーバーに ping を送信します。これを行う 1 つの可能な方法:

var CometProcessor = {
  processMessage:         function(message) {
    console.log(message);
  },
  handleHeartbeatTimeout: function() {
    alert('Heartbeat process timeout');
  },
  handleHeartbeatError: function() {
    alert('Heartbeat process error');
  },

  timeoutPeriod:  10000,      // in milliseconds
  timeoutId:      0,          // will contain an ID of the current 'checking' timeout
  checkHandler:   null,       // will contain a link to XHR object

  checkBeat: function() {
    // storing the reference to created XHR object:
    this.checkHandler = $.ajax({
      url:     your_server_url,
      // set it to the URL of ping script, that will respond instantly

      timeout:     1000,
      // this is configurable, but obviously it makes little sense setting this param
      // higher than `timeoutPeriod`

      success:     $.proxy(function() {
        // so this particular heartbeat request check went through ok,
        // but the next may not be so lucky: we need to schedule another check
        this.timeoutId = window.setTimeout(
          $.proxy(this.checkBeat, this), this.timeoutPeriod);
      }, this),

      error:       $.proxy(function(x, t) {
        if (t === 'timeout') {
          this.handleHeartbeatTimeout();
        }
        else {
          this.handleHeartbeatError();
        }
      }, this)
    });
  },

  message: function(message) {
    // when we receive a message, link is obviously functioning,
    // so we need to stop all the checking procedures
    if (this.checkHandler) {
      this.checkHandler.abort();
      window.clearTimeout(this.timeoutId);
      this.checkHandler = null;
    }
    processMessage(message); // this is where the actual processing takes place

    // when we done with processing, we need to setup the heartbeat again:
    this.timeoutId = window.setTimeout(
      $.proxy(this.checkBeat, this), this.timeoutPeriod);
  }
};

サーバー側でこのオブジェクトを活用するのは非常に簡単です。この例のような行を置き換えるだけです...

Ok.stream(events &> Comet(callback = "parent.cometMessage"))

... これとともに:

Ok.stream(events &> Comet(callback = "parent.CometProcessor.message"))
于 2012-12-24T17:47:11.633 に答える