【原创】socket.io获取在线用户

SocketIoService.java
server.addEventListener("SET-GROUP-CHAT-KEY", Map.class, new DataListener() {
            @Override
            public void onData(final SocketIOClient client, Map data, AckRequest ackRequest) {
                String key = client.get("roomKey");
                //如果原有房间号不为空,且原有的房间号和当前要设置的房间号不相等,则离开原有的房间
                if(key != null && !key.equals(data.get("roomKey").toString())) {
                    client.leaveRoom(key);
                    log.info("ID为" + client.getSessionId() + "离开了房间号为:" + key);
                }
                //如果原有的记间号为空,或者原有的房间号和当前要设置房间不相等,则设置房间号
                if(key == null) {
                    client.joinRoom(data.get("roomKey").toString());//进入房间
                    client.set("roomKey", data.get("roomKey").toString());
                    client.set("userId", data.get("userId").toString());
                    client.set("userName", data.get("userName").toString());
                    client.set("userFace", data.get("userFace").toString());
                    log.info(client.get("roomKey").toString());
                    log.info(client.get("userId").toString());
                    log.info(client.get("userName").toString());
                    log.info(client.get("userFace").toString());
                    log.info("ID为" + client.getSessionId() + "设置了房间号为:" + data.get("roomKey").toString());
                }
                StringBuffer sb = new StringBuffer();
                for (SocketIOClient c : server.getRoomOperations(client.get("roomKey").toString()).getClients()) {
//                    sb.append(c.get("userId")).append(" ").append(c.get("userName")).append(" ").append(c.get("userFace")).append("\n");
                    sb.append(c.getRemoteAddress()).append("\n");
                }
                sb.append("--------------------").append("\n");
                for (SocketIOClient c : server.getRoomOperations(client.get("roomKey").toString()).getClients()) {
                    c.sendEvent(client.get("roomKey").toString(), sb);
                }
            }
        });
        //接收群聊消息
        /*server.addEventListener("GROUP-CHAT-MESSAGE", Map.class, new DataListener() {
            @Override
            public void onData(final SocketIOClient client, Map data, AckRequest ackRequest) {
                for (SocketIOClient c : server.getRoomOperations(data.get("roomKey").toString()).getClients()) {
                    c.sendEvent(data.get("roomKey").toString(), data);
                }
                log.info("向" + data.get("roomKey").toString() + "广播了:" + data);
            }
        });*/
        //下线监听
        server.addDisconnectListener(new DisconnectListener(){
            @Override
            public void onDisconnect(SocketIOClient socketIOClient) {
                log.info(socketIOClient.get("userName").toString() + "离开了...............");
                StringBuffer sb = new StringBuffer();
                for (SocketIOClient c : server.getRoomOperations(socketIOClient.get("roomKey").toString()).getClients()) {
//                    sb.append(c.get("userId")).append(" ").append(c.get("userName")).append(" ").append(c.get("userFace")).append("\n");
                    sb.append(c.getRemoteAddress()).append("\n");
                }
                sb.append("--------------------").append("\n");
                for (SocketIOClient c : server.getRoomOperations(socketIOClient.get("roomKey").toString()).getClients()) {
                    c.sendEvent(socketIOClient.get("roomKey").toString(), sb);
                }
            }
        });
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/base.jsp"%>


你可能感兴趣的:(【原创】socket.io获取在线用户)