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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊