我正在创建java websocket服务器端点。当我的客户端连接到端点时,它保存内存,但是当断开连接时,它不会释放内存。由于什么原因发生?我还为服务器端点添加了代码。请向我建议解决方案
我正在使用spring和javax.websocket。
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.EncodeException;
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 org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.SpringConfigurator;
@Component
@Scope(scopeName = "singleton")
@ServerEndpoint(value = "/chatLoad/{user}", configurator = SpringConfigurator.class, decoders = { MessageDecoder.class }, encoders = { MessageEncoder.class})
public class ServerEndpointLoad {
// List Of Connected Users
Map<String, Session> chatEndpoints = new ConcurrentHashMap<String, Session>();
@OnOpen
public void onOpen(Session session, @PathParam("user") String user) throws IOException, EncodeException, Exception {
System.out.println(this);
System.out.println("connected : " + user);
chatEndpoints.put(user, session);
}
@OnClose
public void onClose(Session session, @PathParam("user") String user) throws EncodeException, IOException {
System.out.println(this);
chatEndpoints.get(user).close();
chatEndpoints.remove(user);
session.close();
System.out.println("session : " + session);
System.out.println("disconnected : " + user);
}
@OnError
public void onError(Session session, Throwable throwable) {
throwable.printStackTrace();
}
@OnMessage
public void onMessage(Session session, Message message) throws Exception {
}
}