import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
HttpSession httpSession = (HttpSession) request.getHttpSession();
sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
上面是配置的configurator放在下面代码使用
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import tool.GetHttpSessionConfig;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint(value = "/tow",configurator = GetHttpSessionConfig.class)
public class tow {
private static final Set<Session> clients = new CopyOnWriteArraySet<>();
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
clients.add(session);
}
@OnMessage
public void onMessage(String message, Session session) throws IOException {
for (Session client : clients) {
if (client != session) { // Avoid sending message back to sender
client.getBasicRemote().sendText(message); // Broadcast message to all clients
}
}
System.out.println(message);
}
@OnClose
public void onClose(Session session) {
clients.remove(session);
}
@OnError
public void onError(Throwable error) {
System.err.println("WebSocket Error: " + error);
}
}
一添加
configurator = GetHttpSessionConfig.class
就报错为什么?
下面是报错图片
不添加
configurator = GetHttpSessionConfig.class就没事正常连接,我的本意是获取httpsession,求解答