Java 带演员的WebSockets与Akka streams直接比较,我应该使用哪一种?
我是一个非常新的游戏框架。我想做的是一个客户机/服务器应用程序,其中Java 带演员的WebSockets与Akka streams直接比较,我应该使用哪一种?,java,playframework,akka,akka-stream,Java,Playframework,Akka,Akka Stream,我是一个非常新的游戏框架。我想做的是一个客户机/服务器应用程序,其中 客户端实时向服务器传输音频(用户必须按下按钮才能通话) 服务器进行识别 服务器实时发回用户当前所说内容的结果 我读了很多关于play框架的书,我想这就是我需要的。但我还不清楚我是否应该直接使用演员或akka流 我做了一些测试(使用演员)和游戏(akka stream),但其中有很多魔法,不一定能理解 顺便说一句,我认为聊天室的例子更适合我的需要。但我甚至无法理解如何在我的服务器上获取数据:(当使用“发送”按钮发送字符串时,我希
HomeController
获取此字符串…我甚至不能这样做
/**
* A very simple chat client using websockets.
*/
public class HomeController extends Controller {
private final Flow<String, String, NotUsed> userFlow;
private final WebJarsUtil webJarsUtil;
@Inject
public HomeController(ActorSystem actorSystem,
Materializer mat,
WebJarsUtil webJarsUtil) {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
LoggingAdapter logging = Logging.getLogger(actorSystem.eventStream(), logger.getName());
//noinspection unchecked
Source<String, Sink<String, NotUsed>> source = MergeHub.of(String.class)
.log("source", logging)
.recoverWithRetries(-1, new PFBuilder().match(Throwable.class, e -> Source.empty()).build());
Sink<String, Source<String, NotUsed>> sink = BroadcastHub.of(String.class);
Pair<Sink<String, NotUsed>, Source<String, NotUsed>> sinkSourcePair = source.toMat(sink, Keep.both()).run(mat);
Sink<String, NotUsed> chatSink = sinkSourcePair.first();
Source<String, NotUsed> chatSource = sinkSourcePair.second();
this.userFlow = Flow.fromSinkAndSource(chatSink, chatSource).log("userFlow", logging);
this.webJarsUtil = webJarsUtil;
}
public Result index() {
Http.Request request = request();
String url = routes.HomeController.chat().webSocketURL(request);
return Results.ok(views.html.index.render(url, webJarsUtil));
}
public WebSocket chat() {
return WebSocket.Text.acceptOrResult(request -> {
if (sameOriginCheck(request)) {
return CompletableFuture.completedFuture(F.Either.Right(userFlow));
} else {
return CompletableFuture.completedFuture(F.Either.Left(forbidden()));
}
});
}
/**
* Checks that the WebSocket comes from the same origin. This is necessary to protect
* against Cross-Site WebSocket Hijacking as WebSocket does not implement Same Origin Policy.
*
* See https://tools.ietf.org/html/rfc6455#section-1.3 and
* http://blog.dewhurstsecurity.com/2013/08/30/security-testing-html5-websockets.html
*/
private boolean sameOriginCheck(Http.RequestHeader request) {
String[] origins = request.headers().get("Origin");
if (origins.length > 1) {
// more than one origin found
return false;
}
String origin = origins[0];
return originMatches(origin);
}
private boolean originMatches(String origin) {
if (origin == null) return false;
try {
URI url = new URI(origin);
return url.getHost().equals("localhost")
&& (url.getPort() == 9000 || url.getPort() == 19001);
} catch (Exception e ) {
return false;
}
}
}
/**
*一个使用WebSocket的非常简单的聊天客户端。
*/
公共类HomeController扩展控制器{
私有最终流用户流;
私人最终WebJarsUtil WebJarsUtil;
@注入
公共家庭控制器(ActorSystem ActorSystem,
物化器垫,
WebJarsUtil WebJarsUtil){
org.slf4j.Logger Logger=org.slf4j.LoggerFactory.getLogger(this.getClass());
LoggingAdapter logging=logging.getLogger(actorSystem.eventStream(),logger.getName());
//未检查
Source Source=MergeHub.of(String.class)
.log(“源”,日志记录)
.recoverWithRetries(-1,new PFBuilder().match(Throwable.class,e->Source.empty()).build());
Sink Sink=BroadcastHub.of(String.class);
Pair sinkSourcePair=source.toMat(sink,Keep.both()).run(mat);
Sink chatSink=sinkSourcePair.first();
Source chatSource=sinkSourcePair.second();
this.userFlow=Flow.fromSinkAndSource(chatSink,chatSource).log(“userFlow”,logging);
this.webJarsUtil=webJarsUtil;
}
公开结果索引(){
Http.Request=Request();
字符串url=routes.HomeController.chat().webSocketURL(请求);
返回Results.ok(views.html.index.render(url,webJarsUtil));
}
公共WebSocket聊天室(){
返回WebSocket.Text.acceptOrResult(请求->{
if(sameOriginCheck(请求)){
返回CompletableFuture.completedFuture(F.other.Right(userFlow));
}否则{
返回CompletableFuture.completedFuture(F.other.Left(禁止());
}
});
}
/**
*检查WebSocket是否来自同一来源。这是保护WebSocket所必需的
*防止跨站点WebSocket劫持,因为WebSocket未实现同源策略。
*
*看https://tools.ietf.org/html/rfc6455#section-1.3及
* http://blog.dewhurstsecurity.com/2013/08/30/security-testing-html5-websockets.html
*/
私有布尔sameOriginCheck(Http.RequestHeader请求){
String[]origins=request.headers().get(“Origin”);
如果(origins.length>1){
//找到多个来源
返回false;
}
字符串原点=原点[0];
返回originMatches(origin);
}
专用布尔源匹配(字符串源){
if(origin==null)返回false;
试一试{
URI url=新的URI(来源);
返回url.getHost().equals(“localhost”)
&&(url.getPort()==9000 | url.getPort()==19001);
}捕获(例外e){
返回false;
}
}
}