每当我尝试从WebSocket服务器访问任何服务时,我的WebSocket连接似乎都消失了。
我尝试调用e.printStackTrace(),这给了我NoSuchElementException:没有值出现错误。
这是我的服务器:
package _Server_Side.Server.config;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import _Server_Side.Server.Entities.User_Entity;
import _Server_Side.Server.Repositories.User_repo;
import _Server_Side.Server.Services.User_Service;
@Service
@ServerEndpoint(value = "/websocket/{username}", configurator = CustomSpringConfigurator.class)
public class WebSocketServer {
private static Map<Session, String> sessionUsernameMap = new HashMap<>();
private static Map<String, Session> usernameSessionMap = new HashMap<>();
private static Map<String, Boolean> isblockeduser = new HashMap<>();
@Autowired
private User_Service user_Service;
@Autowired
private User_repo userRepo;
private final Logger logging = LoggerFactory.getLogger(WebSocketServer.class);
/**
*
* @param session
* @param username
* @throws IOException
*/
@OnOpen
public void onOpen(Session session, @PathParam("username") String username) throws IOException {
logging.info("Entered into Open");
sessionUsernameMap.put(session, username);
usernameSessionMap.put(username, session);
if (username.equals("a")) {
isblockeduser.put("a", true);
}
注释行是导致问题的原因
//User_Entity user = userRepo.findById(2).get();
//System.out.println(user.getGroups().toString());
String message = "User:" + username + " has Joined the Chat";
//System.out.println(user_Service.returngroup(2));
broadcast(message);
}
/**
*
* @param session
* @param username
* @throws IOException
*/
@OnMessage
public void onMessage(Session session, String message) throws IOException {
// Handle new messages
logging.info("Entered into Message: Got Message:" + message);
String username = sessionUsernameMap.get(session);
if (isblocked(session)) {
sendMessageToPArticularUser(username, "[DM] " + username + ": " + "You are blocked from this chat");
} else {
if (message.startsWith("@")) // Direct message to a user using the format "@username <message>"
{
String destUsername = message.split(" ")[0].substring(1); // don't do this in your code!
sendMessageToPArticularUser(destUsername, "[DM] " + username + ": " + message);
sendMessageToPArticularUser(username, "[DM] " + username + ": " + message);
} else // Message to whole chat
{
broadcast(username + ": " + message);
}
System.out.println(user_Service.getAllUsers());
boolean status = user_Service.save_data(message);
}
}
/**
*
* @param session
* @param username
* @throws IOException
*/
@OnClose
public void onClose(Session session) throws IOException {
logging.info("Entered into Close");
String username = sessionUsernameMap.get(session);
sessionUsernameMap.remove(session);
usernameSessionMap.remove(username);
String message = username + " disconnected";
broadcast(message);
}
/**
*
* @param session
* @param username
* @throws IOException
*/
@OnError
public void onError(Session session, Throwable throwable) {
logging.info("Entered into Error");
throwable.printStackTrace() ;
}
/**
*
* @param session
* @param username
* @throws IOException
*/
private void sendMessageToPArticularUser(String username, String message) {
try {
usernameSessionMap.get(username).getBasicRemote().sendText(message);
} catch (IOException e) {
logging.info("Exception: " + e.getMessage().toString());
e.printStackTrace();
}
}
/**
*
* @param session
* @param username
* @throws IOException
*/
private boolean isblocked(Session session) {
String username = sessionUsernameMap.get(session);
if (isblockeduser.containsKey(username) && isblockeduser.get(username) == true)
return true;
return false;
}
/**
*
* @param session
* @param username
* @throws IOException
*/
private static void broadcast(String message) throws IOException {
sessionUsernameMap.forEach((session, username) -> {
synchronized (session) {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
This is the code that I am trying to access from my WebSocket:
public Group_Entity returngroup(int id) {
System.out.println("here");
User_Entity user=getUser(id);
Iterator<Group_Entity> it = user.getGroups().iterator();
if(it.hasNext())
return it.next();
return null;
}
}
我正在尝试获取群组ID,以便可以与其中的群组成员进行单独的聊天