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过期时间的

    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导