在服务器Websocket客户端中无法与服务器连接

时间:2020-02-03 12:01:23

标签: websocket tomcat9

当我使用tomact-9.0.26在本地运行websocket应用程序时工作正常。 但是在服务器中,我部署了应用程序,则websocket无法连接,服务器是ec2实例(aws)。

@ServerEndpoint(value = "/UpCommingNotification/{userId}" )

公共类UpCommingNotification {

private static final Logger logger = LoggerFactory.getLogger(UpCommingNotification.class);
public static Map<String,Session> activeUser = Collections.synchronizedMap(new HashMap<String,Session>());
public UpCommingNotification() {
}

@OnOpen
public void onOpen(@PathParam("userId") String userId, Session session/* ,EndpointConfig config */){
    logger.info("Open Connection for UpCommingNotification and session is " + session.getId() + ",userId:  " + userId);
    session.getUserProperties().put(userId,session);
    activeUser.put(userId,session);
}
@OnClose
public void onClose(@PathParam("userId") String userId,Session session,CloseReason reason){
    logger.info("close socket connection for UpCommingNotification user is " + userId + " and reason is " +  reason.getCloseCode());
    activeUser.remove(userId);
}

@OnMessage
public void onMessage(@PathParam("userId") String userId, String message,Session session) throws IOException, InterruptedException{
    try {
        logger.info("OnOpen UpCommingNotification: " + message);
        SFDAL sfdal = new SFDAL();
        Object obj = JSONValue.parse(message);
        JSONObject jsonObject = (JSONObject) obj;
        AgentNotifacationBean agentNotifacationBean = new AgentNotifacationBean(jsonObject);
        JSONObject notification = sfdal.getUpComingNotification(agentNotifacationBean);
        if(activeUser.containsKey(agentNotifacationBean.getUserId()) && !notification.isEmpty()) {
            Future<Void> deliveryTracker = session.getAsyncRemote().sendText(notification.toString());
            logger.info("msg: " + notification.toString());
            logger.info("msg sent status: " + deliveryTracker.isDone());
        }

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

@OnError
public void onError(Throwable e){
}

}

0 个答案:

没有答案