[英]Play framework 2.5.x Web Socket Java
I am following the instructions in the official documentation of Play framework 2.5.x to Java Websockets, I created a controller with this function 我按照Play框架2.5.x的官方文档中的说明访问Java Websockets,我创建了一个具有此功能的控制器
public static LegacyWebSocket<String> socket() {
return WebSocket.withActor(MyWebSocketActor::props);
}
And an Actor class MyWebSocketActor: 还有一个Actor类MyWebSocketActor:
public class MyWebSocketActor extends UntypedActor {
public static Props props(ActorRef out) {
return Props.create(MyWebSocketActor.class, out);
}
private final ActorRef out;
public MyWebSocketActor(ActorRef out) {
this.out = out;
}
public void onReceive(Object message) throws Exception {
if (message instanceof String) {
out.tell("I received your message: " + message, self());
}
}
}
Then the app is started I try to connect at ws://localhost:9000 as is written in the official documentation: 然后应用程序启动我尝试连接到ws:// localhost:9000,正如官方文档中所写:
Tip: You can test your WebSocket controller on https://www.websocket.org/echo.html .
提示:您可以在https://www.websocket.org/echo.html上测试WebSocket控制器。 Just set the location to ws://localhost:9000.
只需将位置设置为ws:// localhost:9000。
But the web socket seems unreachable, how can I test it? 但是网络套接字似乎无法访问,我该如何测试呢?
Thanks 谢谢
In order to handle WebSocket connections, you also have to add a route in your routes
file. 为了处理WebSocket连接,您还必须在
routes
文件中添加routes
。
GET /ws controllers.Application.socket()
Then your WebSocket endpoint will be ws://localhost:9000/ws
- use it for testing with the echo service. 然后你的WebSocket端点将是
ws://localhost:9000/ws
- 用它来测试echo服务。
Finally with the help of Anton I solved it! 最后在安东的帮助下我解决了它! First: remove static from socket() method
首先:从socket()方法中删除static
public LegacyWebSocket<String> socket() {
return WebSocket.withActor(MyWebSocketActor::props);
}
Then add an endpoint in routes file for the socket() method 然后在路由文件中为socket()方法添加端点
GET /ws controllers.HomeController.socket()
At this point you have to start the application with SSL/TLS in this way, for example: 此时,您必须以这种方式使用SSL / TLS启动应用程序,例如:
activator run -Dhttps.port=9443
In websocket.org/echo.html insert wss://localhost:9443/ws
in Location field and it connects to websocket! 在websocket.org/echo.html中,在位置字段中插入
wss://localhost:9443/ws
,它连接到websocket!
Furthermore if I visit https://localhost:9443/ws
I continue to obtain the message 此外,如果我访问
https://localhost:9443/ws
我继续获取该消息
Upgrade to WebSocket required
需要升级到WebSocket
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.