我是Play框架的新手,Play的文档并不全面
我试图在这个文档的帮助下编写测试websocket: https://www.playframework.com/documentation/2.6.x/JavaWebSockets
控制器
package controllers.websockets;
import akka.actor.ActorSystem;
import akka.stream.Materializer;
import play.libs.streams.ActorFlow;
import play.mvc.Controller;
import play.mvc.WebSocket;
import javax.inject.Inject;
public class TestWebSocketController extends Controller {
private final ActorSystem actorSystem;
private final Materializer materializer;
@Inject
public TestWebSocketController(ActorSystem actorSystem, Materializer materializer) {
this.actorSystem = actorSystem;
this.materializer = materializer;
}
public WebSocket socket() {
return WebSocket.Text.accept(request ->
ActorFlow.actorRef(TestWebSocketActor::props,
actorSystem, materializer
)
);
}
}
演员:
package controllers.websockets;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
public class TestWebSocketActor extends AbstractActor {
public static Props props(ActorRef out) {
return Props.create(TestWebSocketActor.class, out);
}
private final ActorRef out;
public TestWebSocketActor(ActorRef out) {
this.out = out;
}
@Override
public AbstractActor.Receive createReceive() {
return receiveBuilder()
.match(String.class, message ->
out.tell("I received your message: " + message, self())
)
.build();
}
}
CONF /路线:
# WebSockets
GET /ws controllers.websockets.TestWebSocketController.socket()
我尝试使用Simmple WebSocket Plugin for Chrome https://www.websocket.org/echo.html的帮助,使用这样的url:ws:// localhost:9000 / ws,但出现了错误消息:
WebSocket connection to 'ws://localhost:9000/ws' failed: Error during WebSocket handshake: Unexpected response code: 404
CLOSED: ws://localhost:9000/ws