public class TestWebSocket{ private Session session;
private HttpSession httpSession;
@OnOpen
public void onOpen(Session session, EndpointConfig config) throws IOException, InterruptedException {
System.out.println("open ws...");
this.session = session;
this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
System.out.println(httpSession);
if(httpSession != null){
/*ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(httpSession.getServletContext());
this.webSocketService = (WebSocketService)ctx.getBean("webSocketService"); */
System.out.println("is not null");
this.session.getBasicRemote().sendText("back from houtai");
}
}
@OnError
public void onError(Throwable t) throws Throwable{
System.out.println("error...");
}
@OnClose
public void onClose() throws IOException, InterruptedException {
}
@OnMessage
public void onMessage(String message ,Session session) throws IOException{
System.out.println(message + "from " + session.getId());
//sendMessage(message);
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText("back from houtai");
}
}这样写完后,假如我在某个action中处理了某些逻辑后,需要发送消息给客户端,要如何调用websocket呢
private HttpSession httpSession;
@OnOpen
public void onOpen(Session session, EndpointConfig config) throws IOException, InterruptedException {
System.out.println("open ws...");
this.session = session;
this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
System.out.println(httpSession);
if(httpSession != null){
/*ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(httpSession.getServletContext());
this.webSocketService = (WebSocketService)ctx.getBean("webSocketService"); */
System.out.println("is not null");
this.session.getBasicRemote().sendText("back from houtai");
}
}
@OnError
public void onError(Throwable t) throws Throwable{
System.out.println("error...");
}
@OnClose
public void onClose() throws IOException, InterruptedException {
}
@OnMessage
public void onMessage(String message ,Session session) throws IOException{
System.out.println(message + "from " + session.getId());
//sendMessage(message);
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText("back from houtai");
}
}这样写完后,假如我在某个action中处理了某些逻辑后,需要发送消息给客户端,要如何调用websocket呢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货