Playframework Websocket从服务器到浏览器的实时流式传输 [英] Playframework websocket live streaming from server to browser
问题描述
我正在尝试使用playframework将数据从Web服务器连续流式传输到浏览器.我根据示例文档
I am trying to continuously stream data from webserver to browser using playframework. I created a sample code based from example documentation
这是我的代码段
public static WebSocket<String> sockHandler() {
return new WebSocket<String>() {
// called when the websocket is established
public void onReady(WebSocket.In<String> in, final WebSocket.Out<String> out) {
// register a callback for processing instream events
in.onMessage(new F.Callback<String>() {
public void invoke(String event) {
Logger.info(event);
}
});
int i = 0;
while(i<20){
Logger.info("Sending message ");
try{
Thread.sleep(1000);
}catch (Exception e){
Logger.error("Exception " ,e);
}
out.write("Sending message " + i++ + "\n");
}
}
};
}
在浏览器上,每次迭代后都不会立即接收写入的消息. while循环完成后,浏览器会一次全部接收到消息.
On the browser, the messages written to out is not received immediately after each iteration. Messages are received in browsers all at once after while loop completes.
是否有一种方法可以在每次迭代后刷新流.还是有更好的示例/文档来将实时数据从websocket服务器流式传输到浏览器.?
Is there a way to flush out stream after each iteration..? Or is there a better example/documentation to stream live data from websocket server to browser..?
谢谢, 拉贾.
推荐答案
我强烈建议您看一下它使用Websockets和Akka演员. Actor系统不是很难理解, Akka文档是一个很好的系统.开始的地方!
It uses Websockets and Akka actors. The Actor system is not very difficult to understand, the Akka documentation is a good place to start !
这篇关于Playframework Websocket从服务器到浏览器的实时流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!