以下代码在Play框架2.3中无法工作:
controllers.NotificationHandler.sendNotification():
JS代码:
controllers.NotificationHandler.sendNotification():
def sendNotification = Action {
request =>
val currentdate = new java.util.Date()
Ok("data: " + currentdate.toString())
.as( "text/event-stream")
.withHeaders("Content-Type"->"text/event-stream")
.withHeaders("Cache-Control"->"no-cache")
.withHeaders("Connection"->"keep-alive")
}
JS代码:
代码:
var event = new EventSource("@controllers.NotificationHandler.sendNotification()");
event.addEventListener('message', function( event ) {
alert(event.data);
} );