EventSource和Internet Explorer

时间:2014-03-24 10:57:25

标签: html5 scala playframework-2.0 server-sent-events

我有以下服务器端播放代码,以便为HTML5 EventSources提供端点。

package controllers

import scala.util.matching
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.iteratee.{Concurrent, Enumeratee}
import play.api.libs.EventSource
import play.api.libs.concurrent.Execution.Implicits._

object Application extends Controller {

  /** Central hub for distributing chat messages */
  val (eventOut, eventChannel) = Concurrent.broadcast[JsValue]

  /** Enumeratee for filtering messages based on eventSpec */
  def filter(eventSpec: String) = Enumeratee.filter[JsValue] {
    json: JsValue => ("(?i)" + eventSpec).r.pattern.matcher((json \ "eventSpec").as[String]).matches
  }

  /** Enumeratee for detecting disconnect of SSE stream */
  def connDeathWatch(addr: String): Enumeratee[JsValue, JsValue] = 
    Enumeratee.onIterateeDone{ () => println(addr + " - SSE disconnected") }

  /** Controller action serving activity based on eventSpec */
  def events = Action { req =>
    println(req.remoteAddress + " - connected and subscribed for '" + eventSpec +"'")
    Ok.feed(eventOut
      &> filter(eventSpec) 
      &> Concurrent.buffer(50) 
      &> connDeathWatch(req.remoteAddress)
      &> EventSource()
    ).as("text/event-stream").withHeaders(
      "Access-Control-Allow-Origin" -> "*",
      "Access-Control-Allow-Methods" -> "GET",
      "Access-Control-Allow-Headers" -> "Content-Type"
    )
  }

}

我的路线看起来像那样

  

GET / events controllers.Application.events

当Chrome浏览器通过EventSource对象附加自身时,此服务器可以正常工作。由于IE不支持EventSources,我使用的是this polyfill库。现在的问题是:IE正确订阅事件,因为我看到“连接和订阅”日志输出,但是一旦事件应该传递给此连接,它就会记录“SSE已断开连接”。我在这里错过了什么?一些http标题?

1 个答案:

答案 0 :(得分:0)

看起来您可能正在使用CORS。 (您正在发送CORS标头。)如果是这样,那可能是问题,因为您使用的polyfill不支持CORS。如果您需要CORS,可以使用this polyfill instead