我正在使用 Play 框架 2.1.x
我可以成功创建一个 websocket,但它会在向客户端发送数据时迅速关闭。1.x 中似乎有一种使用 await 函数的机制——但我理解它已被弃用,因为 WebSocketController 现在已被弃用。如何确保我的 websocket 的“生命”
public static WebSocket<String> loggedInSocket() {
try {
final Http.Session session = session();
String username = session(AppConstants.USERNAME);
connection = getConnection(username);
connection = XMPPConnectionHandler.performLogin(xmppLogin.getLoggedinuser().getUsername(),xmppLogin.getLoggedinuser().getPassword(),connection);
getAllData();
} catch (Exception e) {
e.printStackTrace();
Logger.error(e.getLocalizedMessage());
}
return new WebSocket<String>() {
public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {
System.out.println("ready");
in.onMessage(new F.Callback() {
public void invoke(String event) {
System.out.println(event);
}
@Override
public void invoke(Object a) throws Throwable {
// TODO Auto-generated method stub
}
});
in.onClose(new F.Callback0() {
public void invoke() {
System.out.println("Disconnected");
}
});
out.write("Hello");
}
};
}