我是玩框架的新手。我想做的是一个客户端/服务器应用程序,其中
我读了很多有关游戏框架的文章,我认为WebSocket是我所需要的。但是对我来说还不清楚,是否应该直接使用actors或akka流?
我在play chatroom example(使用演员)和戏剧stock market example(akka流)中进行了一些测试,但是里面有很多魔术,不确定是否会理解。
顺便说一句,作为附带说明,我认为聊天室示例更适合我的需求。但是我什至无法理解如何在服务器上获取数据:(当使用时,单击“发送”按钮发送字符串,我希望我的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;
}
}
}