package com.yixin.xv.admin.controller;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.nutz.ioc.loader.annotation.IocBean;
@IocBean
@ServerEndpoint(value = "/websocket", configurator=NutIocWebSocketConfigurator.class)
public class MyWebsocket extends Endpoint {
static Map<String, Session> sessionMap = new Hashtable<String, Session>();
private HttpSession httpSession;
@Override
public void onOpen(Session session, EndpointConfig config) {
final RemoteEndpoint.Basic basic = session.getBasicRemote();
// HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
this.httpSession = (HttpSession) config.getUserProperties()
.get(HttpSession.class.getName());
// sessionMap.put(session.getId(), session);
try {
basic.sendText("open success on server");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 收到客户端消息时触发
*
* @param relationId
* @param userCode
* @param message
* @return
*/
@OnMessage
public String onMessage(Session session, String message) {
System.out.println("message=="+message);
sessionMap.put(message, session);
return "Got you msg !" + message;
}
/**
* 异常时触发
*
* @param relationId
* @param userCode
* @param session
*/
@OnError
public void onError(Throwable throwable, Session session) {
sessionMap.remove(session);
System.out.println("pathParams:" + session.getPathParameters());
System.out.println("requestParams" + session.getRequestParameterMap());
System.out.print("onError" + throwable.toString());
}
/**
* 关闭连接时触发
*
* @param relationId
* @param userCode
* @param session
*/
@OnClose
public void onClose(Session session) {
sessionMap.remove(httpSession.getId());
System.out.println("pathParams:" + session.getPathParameters());
System.out.println("requestParams" + session.getRequestParameterMap());
System.out.print("onClose ");
}
}
怀疑是不是过滤器的问题啊 可是 在shiro.ini 添加了 不走过滤器 貌似没起作用