这是我的Jetty Web-socket服务器实现,我能够创建服务器并可以从客户端接收消息。
我收到方法
的消息public void onText(Session session, String message)
现在从这个方法我怎样才能调用我的主文件(ToUpperWebSocketServletMain)方法。无论如何我可以传递pas主类(ToUpperWebSocketServletMain)参考?当我在WebSocket中收到消息时,我可以使用它吗?
public class ToUpperWebSocketServletMain {
public static void main(String[] args) {
Server server = new Server(6060);
WebSocketHandler handler1 = new WebSocketHandler()
{
@Override
public void configure(WebSocketServletFactory factory) {
System.out.println("WebSocket TOUPPER initialized");
factory.register(ToUpperWebSocket.class);
}
};
ContextHandler context1 = new ContextHandler();
context1.setContextPath("/ToUpper/");
context1.setHandler(handler1);
ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
contextHandlers.setHandlers(new Handler[] {context1} );
server.setHandler(contextHandlers);
try {
server.start();
server.join();
System.out.println("WebSocket Started");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@WebSocket
public class ToUpperWebSocket {
@OnWebSocketMessage
public void onText(Session session, String message) throws IOException {
System.out.println("Message received:" + message);
if (session.isOpen()) {
String response = message.toUpperCase();
session.getRemote().sendString(response);
}
}
@OnWebSocketConnect
public void onConnect(Session session) throws IOException {
System.out.println("ToUpperWebSocket"+session.getRemoteAddress().getHostString() + " connected!");
}
@OnWebSocketClose
public void onClose(Session session, int status, String reason) {
System.out.println("ToUpperWebSocket"+session.getRemoteAddress().getHostString() + " closed!");
}
}