jiexianerzou 2017-03-02 03:43 采纳率: 0%
浏览 1588
已结题

使用tomcat8写了个webSocket聊天室,出现如下异常

图片说明
如图,在本地运行无异常,上传至阿里云服务器后开始时无异常,等待一段时间(推测是session timeout后)出现如图异常

package com.project.webSocket;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
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.ServerEndpoint;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.project.service.Manager;
import com.project.service.impl.ManagerImpl;
import com.project.utils.GetHttpSessionConfigurator;

//@ServerEndpoint注解表示将 WebSocket服务端运行在 以ws://开头的访问端点
@ServerEndpoint(value = "/SocketServer",configurator = GetHttpSessionConfigurator.class)
public class SocketServer{
private static final long serialVersionUID = 1L;
//将httpSession,socketSession放在一个map里,代表一个用户
private static final HashMap onlineUsers = new HashMap();
private static int onlineCount = 0;
//定义客服人员的名字(对应cookie--user_id)
private static String customerService1 = "A002";
private static String customerService2 = "A003";
private HttpSession httpSession;
/**
* WebSocket Session
*/
private Session session;

//使用 ServerEndpoint 注释的类必须有一个公共的无参数构造函数
public SocketServer() {
}

/**
 * 打开连接
 * 
 * @param session
 */
@OnOpen
public void onOpen(Session session,EndpointConfig config) {
    this.session = session;
    this.httpSession = (HttpSession)config.getUserProperties().get(HttpSession.class.getName());
    httpSession.setMaxInactiveInterval(60000);
    if(httpSession.getAttribute("user") != null){
        onlineUsers.put(httpSession, this);
    }
    addOnlineCount();
    System.out.println("++++当前人数" + onlineUsers.size());
    System.out.println(httpSession.getAttribute("user"));
}

/**
 * 关闭连接
 */
@OnClose
public void onClose() {
    onlineUsers.remove(httpSession,this);//从map中删除
    subOnlineCount();
    System.out.println("----当前人数" + onlineUsers.size());
}

     /**
 * 错误信息响应
 * 
 * @param throwable
 */
@OnError
public void onError(Throwable throwable) {
    System.out.println("连接异常");
    System.out.println(throwable.getMessage());
}


    请问是什么问题造成的?
  • 写回答

1条回答 默认 最新

  • Cicyer2513 2017-03-02 03:40
    关注

    客户端也需要设置session过期时间的

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug