我正在尝试遵循官方文档中的JavaWebSocket教程。
有这个演员:
import akka.actor.*;
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());
}
}
}
这是websocket:
public static LegacyWebSocket<String> socket() {
return WebSocket.withActor(MyWebSocketActor::props);
}
这是我的控制器
@Singleton
public class MessagesController extends BaseController implements CurrentUser {
private UserProvider userProvider;
private ActorSystem actorSystem;
private Materializer materializer;
private Configuration configuration;
ActorRef websocketactor;
@Inject
public MessagesController(final UserProvider userProvider,
ActorSystem actorSystem,
Materializer materializer,
Configuration configuration
) {
this.userProvider = userProvider;
this.actorSystem = actorSystem;
this.materializer = materializer;
this.configuration = configuration;
this.websocketactor = actorSystem.actorOf(); // What goes in here ?
}
在init过程之后,我想从控制器方法向actor发送消息。
this.websocketactor = actorSystem.actorOf(MyWebSocketActor.props()); // this line is giving me errors because I don't know what goes in there.
它可能是ActorRef out,这是我的websocket,但我怎么指定呢?
我刚刚在另一篇文章中回答了同样的问题,但以防万一,这里是:
你可以使用lambda,这里有一个简单的例子:
public LegacyWebSocket<String> socket(String token) {
return WebSocket.withActor(actorRef -> WSActor.props(actorRef,token));
}