2014-03-24 164 views
4

我有以下服務器端播放代碼,以便爲HTML5 EventSources提供端點。EventSource和Internet Explorer

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 /事件controllers.Application.events

當Chrome瀏覽器通過一個EventSource的物體連接本身這臺服務器工作完全正常。由於IE不支持EventSources,因此我使用了this填充庫。現在的問題是:IE正確訂閱了事件,因爲我看到'連接和訂閱'日誌輸出,但只要事件應該傳遞給此連接,它就會記錄'SSE斷開連接'。我在這裏錯過了什麼?一些http頭文件?

回答

0

看起來您可能正在使用CORS。 (你正在發送CORS頭文件。)如果是這樣,那可能是問題所在,因爲你使用的polyfill不支持CORS。如果您需要CORS,則可以使用this polyfill instead