WebSocket的使用(并发登录问题)

相对于HTTP这种非持久的协议来说,Websocket是一个持久化的协议(注H5新特性,java7开始才有的JSR-356:Java API for WebSocket规范)。

已处理并发登录为例:

使用websocket open拿到httpsession:

package com.chinasofti.config;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

public class HttpSessionConfigurator extends Configurator{

	@Override
	public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
		HttpSession httpSession = (HttpSession) request.getHttpSession();
		if(httpSession != null){
			sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
		}
	}

}

从httpsession中获取登录账户,并添加到websocket中,登录时查看httpsession中是否已存在账户:

package com.chinasofti.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.alibaba.fastjson.JSON;
import com.chinasofti.config.HttpSessionConfigurator;
import com.chinasofti.model.Account;
import com.chinasofti.pageModel.base.Msg;

@ServerEndpoint(value = "/ws/websocket", configurator = HttpSessionConfigurator.class)
public class WebSocketServer {

	private Account account;

	private HttpSession httpSession;

	public static Map<String, Session> sessionMap = new HashMap<String, Session>();

	public static Map<String, HttpSession> httpSessionMap = new HashMap<String, HttpSession>();

	@OnOpen
	public void start(Session session, EndpointConfig config) {
		httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
		account = (Account) httpSession.getAttribute("account");
		httpSessionMap.put(account.getAccountid(), httpSession);
		sessionMap.put(account.getAccountid(), session);
	}

	@OnClose
	public void end() {
		try {
			this.sessionInvalidate();
		} catch (Exception e) {
		} finally {
			httpSessionMap.remove(account.getAccountid());
		}
		sessionMap.remove(account.getAccountid());
	}

	@OnError
	public void onError(Throwable t) throws Throwable {
		System.out.println("异常:" + t.getMessage());
	}

	public static void sendMessage(String userid, Msg msg) {
		Session session = sessionMap.get(userid);
		try {
			session.getBasicRemote().sendText(JSON.toJSONString(msg));
		} catch (IOException e) {
			System.out.println("sendMessage异常:" + e.getMessage());
		}
	}

	public static void sendToAllUsersMessage(Msg msg) {
		Set<String> set = sessionMap.keySet();
		for (String key : set) {
			Session session = sessionMap.get(key);
			try {
				session.getBasicRemote().sendText(JSON.toJSONString(msg));
			} catch (IOException e) {
				System.out.println("sendToAllUsersMessage异常:" + e.getMessage());
			}
		}
	}

	private void sessionInvalidate() throws Exception {
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					if (httpSessionMap.get(account.getAccountid()) == null) {
						httpSession.invalidate();
					}
				} catch (Exception e) {
				}
			}
		}.start();
	}

	// 判断某个账户是否在线
	public static boolean isOnline(String userid) {
		if (httpSessionMap.get(userid) != null) {
			return true;
		}
		return false;
	}

}
HttpSession mapSession=WebSocketServer.httpSessionMap.get(account.getAccountid());

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值