public void addClient(SocketIOClient client) { allClients.put(client.getSessionId(), client); }
public <T> void sendEvent(String name, Object data, SocketIOClient excludedClient, BroadcastAckCallback<T> ackCallback) { for (SocketIOClient client : clients) { if (client.getSessionId().equals(excludedClient.getSessionId())) { continue; } client.sendEvent(name, ackCallback.createClientCallback(client), data); } ackCallback.loopFinished(); }
@Override public void disconnect() { for (SocketIOClient client : clients) { client.disconnect(); } }
public void sendEvent(String name, SocketIOClient excludedClient, Object... data) { Packet packet = new Packet(PacketType.MESSAGE); packet.setSubType(PacketType.EVENT); packet.setName(name); packet.setData(Arrays.asList(data)); for (SocketIOClient client : clients) { if (client.getSessionId().equals(excludedClient.getSessionId())) { continue; } client.send(packet); } dispatch(packet); }
public void onDisconnect(SocketIOClient client) { Set<String> joinedRooms = client.getAllRooms(); allClients.remove(client.getSessionId()); leave(getName(), client.getSessionId()); storeFactory.pubSubStore().publish(PubSubType.LEAVE, new JoinLeaveMessage(client.getSessionId(), getName(), getName())); for (String joinedRoom : joinedRooms) { leave(roomClients, joinedRoom, client.getSessionId()); } clientRooms.remove(client.getSessionId()); try { for (DisconnectListener listener : disconnectListeners) { listener.onDisconnect(client); } } catch (Exception e) { exceptionListener.onDisconnectException(e, client); } }
/** * 添加disconnect事件,客户端断开连接时调用,刷新客户端信息 * * @param client 客户端对象 */ @OnDisconnect public void onDisconnect(SocketIOClient client) { if (client != null) { String token = client.getHandshakeData().getSingleUrlParam("token"); // 模拟用户id 和token一致 String userId = client.getHandshakeData().getSingleUrlParam("token"); UUID sessionId = client.getSessionId(); dbTemplate.deleteByUserId(userId); log.info("客户端断开连接,【token】= {},【sessionId】= {}", token, sessionId); client.disconnect(); } else { log.error("客户端为空"); } }
@OnConnect public void onConnect(SocketIOClient socketIOClient) { // String token = socketIOClient.getHandshakeData().getHttpHeaders().get(HeaderFactory.HEADER_KEY_USER_TOKEN); String token = socketIOClient.getHandshakeData().getSingleUrlParam(HeaderFactory.HEADER_KEY_USER_TOKEN); if (StringUtil.isEmpty(token)) { throw new ZJHRuntimeException(""); } UserHeaderContainer container = UserHeaderContainer.resolveUserCookie(token); if (container == null) { throw new ZJHRuntimeException(""); } User user = userCache.get(new UserCacheKey(container.getUserId(), container.getUuid())); if (user == null) { throw new ZJHRuntimeException(""); } logger.info("{} connect!!, userId:{}", user.getUsername(), user.getId()); UserInCache describe = new UserInCache(); describe.setUserId(user.getId()); describe.setUsername(user.getUsername()); clientMap.put(socketIOClient.getSessionId().toString(), describe); socketIOClient.sendEvent("connectedSocket", describe); }
@OnConnect public void onConnect(SocketIOClient client) { if (client != null) { String username = client.getHandshakeData().getSingleUrlParam("username"); String password = client.getHandshakeData().getSingleUrlParam("password"); String sessionId = client.getSessionId().toString(); logger.info("连接成功, username=" + username + ", password=" + password + ", sessionId=" + sessionId); } else { logger.error("客户端为空"); } }
/** * 添加@OnDisconnect事件,客户端断开连接时调用,刷新客户端信息 * * @param client 客户端 */ @OnDisconnect public void onDisconnect(SocketIOClient client) { logger.info("客户端断开连接, sessionId=" + client.getSessionId().toString()); client.disconnect(); }
public <T> void sendEvent(String name, Object data, BroadcastAckCallback<T> ackCallback) { for (SocketIOClient client : clients) { client.sendEvent(name, ackCallback.createClientCallback(client), data); } ackCallback.loopFinished(); }
@OnEvent(value="createRoom") public void createRoom(SocketIOClient client, Object data, AckRequest request) { UserInCache userInCache = SocketConnectedHandler.clientMap.get(client.getSessionId().toString()); RoomContext room = new RoomContext(userInCache); userInCache.setRoomId(room.getRoomId()+""); RoomContext.roomMap.put(room.getRoomId(), room); client.joinRoom(userInCache.getRoomId()); request.sendAckData(room); logger.info("{} create room({}) success.", userInCache.getUserId()+userInCache.getUsername(), userInCache.getRoomId()); }
@Override public void onConnect(SocketIOClient client) { logger.info("client connected!"); String username=client.getHandshakeData().getSingleUrlParam("username"); client.set("username",username); client.joinRoom("/user/private/"+username); } });
@Override public void onDisconnect(SocketIOClient client) { for(String room:client.getAllRooms()){ client.getNamespace().getRoomOperations(room).sendEvent("user:leave",client.get("username"),room); } } });
@OnEvent(value="quitRoom") public void quitRoom(SocketIOClient client, Object data, AckRequest request) { UserInCache userInCache = SocketConnectedHandler.clientMap.get(client.getSessionId().toString()); String roomId = userInCache.getRoomId(); ResponseBean bean = ResponseBean.crtSuccessBean(); client.leaveRoom(roomId+""); userInCache.setRoomId(""); logger.info("{} exit room {}",userInCache.getUserId()+userInCache.getUsername(), userInCache.getRoomId());
/** * 加入群聊 * * @param client 客户端 * @param request 请求 * @param data 群聊 */ @OnEvent(value = Event.JOIN) public void onJoinEvent(SocketIOClient client, AckRequest request, JoinRequest data) { log.info("用户:{} 已加入群聊:{}", data.getUserId(), data.getGroupId()); client.joinRoom(data.getGroupId()); server.getRoomOperations(data.getGroupId()).sendEvent(Event.JOIN, data); }
private void dispatch(Packet packet) { Map<String, Set<String>> namespaceRooms = new HashMap<String, Set<String>>(); for (SocketIOClient socketIOClient : clients) { Namespace namespace = (Namespace)socketIOClient.getNamespace(); Set<String> rooms = namespace.getRooms(socketIOClient); Set<String> roomsList = namespaceRooms.get(namespace.getName()); if (roomsList == null) { roomsList = new HashSet<String>(); namespaceRooms.put(namespace.getName(), roomsList); } roomsList.addAll(rooms); } for (Entry<String, Set<String>> entry : namespaceRooms.entrySet()) { for (String room : entry.getValue()) { storeFactory.pubSubStore().publish(PubSubType.DISPATCH, new DispatchMessage(room, packet, entry.getKey())); } } }
public void onDisconnect(SocketIOClient client) { Set<String> joinedRooms = client.getAllRooms(); allClients.remove(client.getSessionId()); leave(getName(), client.getSessionId()); storeFactory.pubSubStore().publish(PubSubType.LEAVE, new JoinLeaveMessage(client.getSessionId(), getName(), getName())); for (String joinedRoom : joinedRooms) { leave(roomClients, joinedRoom, client.getSessionId()); } clientRooms.remove(client.getSessionId()); try { for (DisconnectListener listener : disconnectListeners) { listener.onDisconnect(client); } } catch (Exception e) { exceptionListener.onDisconnectException(e, client); } }
/** * 添加connect事件,当客户端发起连接时调用 * * @param client 客户端对象 */ @OnConnect public void onConnect(SocketIOClient client) { if (client != null) { String token = client.getHandshakeData().getSingleUrlParam("token"); // 模拟用户id 和token一致 String userId = client.getHandshakeData().getSingleUrlParam("token"); UUID sessionId = client.getSessionId(); dbTemplate.save(userId, sessionId); log.info("连接成功,【token】= {},【sessionId】= {}", token, sessionId); } else { log.error("客户端为空"); } }
@OnDisconnect public void onDisconnect(SocketIOClient client) { logger.info("客户端断开连接, sessionId=" + client.getSessionId().toString()); client.disconnect(); }
&& Constants.usernameSocketIdMap.containsKey(topic.getUserId())) { Constants.websocketUserMap.get(Constants.usernameSocketIdMap.get(topic.getUserId())).getClient() .sendEvent("notifications", String.format(emailTitle, topic.getTitle(), user.getUsername()));