【Java】在springboot里面使用HttpSessionConfigurator报错
package cn.springbootlearn.socket;import cn.spring.inter.bean.ResponseData;
import cn.spring.inter.bean.RoundData;
import cn.spring.inter.bean.User;
import cn.spring.inter.bean.UserGridInfo;
import cn.spring.inter.utils.ChessDataCompute;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpSession;
import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
@Controller
@ServerEndpoint(value="/websocket/{info}", configurator = HttpSessionConfigurator.class)
public class Socket {
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
/*websocket 客户端会话 通过Session 向客户端发送数据*/
private Session session;
/*线程安全set 存放每个客户端处理消息的对象*/
private static CopyOnWriteArraySet<Socket> webSocketSet = new CopyOnWriteArraySet();
/*websocket 连接建立成功后进行调用*/
private static Map<String, String> userList = new HashMap<String, String>();
private static SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//创建时间格式对象
/**
* 功能:创建一个房间的集合,用来存放房间
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
* 若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
*/
private static ConcurrentHashMap<String, ConcurrentHashMap<String, Socket>> roomList = new ConcurrentHashMap<String,ConcurrentHashMap<String, Socket>>();
//重新加入房间的标示;
private int rejoin = 0;
static {
roomList.put("room1", new ConcurrentHashMap<String, Socket>());
roomList.put("room2", new ConcurrentHashMap<String, Socket>());
roomList.put("wuxian20200424", new ConcurrentHashMap<String, Socket>());
}
private static Map<String, String> recordMap = new HashMap<String,String>();
Map<String, String[]> userMap = new HashMap<String,String[]>();
@OnOpen
public void onOpen(@PathParam(value = "info") String param, Session session, EndpointConfig config) {
this.session = session;
String flag = param.split("[|]")[0]; //标识
String roomId = param.split("[|]")[1]; //成员名
// 获取当前用户的session
System.out.println("sessionId = " + session.getId());
HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
User user = (User) httpSession.getAttribute("user"); // 获得当前用户信息
}
}
package cn.springbootlearn.socket;import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
/**
* @author abbottLiu
* @Description
* @date 2020/04/28
*/
public class HttpSessionConfigurator extends ServerEndpointConfig.Configurator{
@Override
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
HttpSession session = (HttpSession) request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(), session);
}
}
回答
没有示例这边不好还原测试。
倒是在网上找到了一个似乎相近的问题答案,你可以尝试看看。
Springboot-WebSocket获取HttpSession问题
以上是 【Java】在springboot里面使用HttpSessionConfigurator报错 的全部内容, 来源链接: utcz.com/a/89010.html