Java Code Examples for io.vertx.ext.web.handler.sockjs.BridgeEvent#socket()

The following examples show how to use io.vertx.ext.web.handler.sockjs.BridgeEvent#socket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OfflineChainHandler.java    From slime with MIT License 5 votes vote down vote up
@Override
	public boolean handle(BridgeEvent event) throws EventBridgeChainException {
		boolean isHandle = Boolean.FALSE;
		
		if(BridgeEventType.SOCKET_CLOSED == event.type()) {
			Vertx vertx = VertxHolder.getVertx();
			
			isHandle = Boolean.TRUE;
			
			SockJSSocket sockJSSocket = event.socket();
			
			// notify user offline
			String address = "topic/chat/offline";
			String userId = sockJSSocket.headers().get(WebSocketSessionHolder.USER_KEY);
			
			WebSocketSessionHolder.remove(userId);

			RedisSockjsClient.PUBLISH(address,
		            new JsonObject()
	                .put("userId", userId));
					
//			vertx.eventBus().publish(address,
//		            new JsonObject()
//		                .put("userId", userId));
			
		}
		
		return isHandle;
	}
 
Example 2
Source File: ChatMessageChainHandler.java    From slime with MIT License 5 votes vote down vote up
@Override
	public boolean handle(BridgeEvent event) throws EventBridgeChainException {
		boolean isHandle = Boolean.FALSE;
		
		if(BridgeEventType.PUBLISH == event.type()) {
			Vertx vertx = VertxHolder.getVertx();
			SockJSSocket sockJSSocket = event.socket();
			Map<String, Object> rawMessage = event.getRawMessage().getMap();
			
			String senderId = sockJSSocket.headers().get(WebSocketSessionHolder.USER_KEY);
			String address = (String) rawMessage.get("address");
			String msg = (String) rawMessage.get("body");
			
			RedisSockjsClient.PUBLISH(address,
		            new JsonObject()
		                .put("message", msg)
		                .put("sender", senderId));
//			vertx.eventBus().publish(address,
//		            new JsonObject()
//		                .put("message", msg)
//		                .put("sender", senderId));
			
			isHandle = Boolean.TRUE;
			
		}
		
		return isHandle;
	}
 
Example 3
Source File: LoginChainHandler.java    From slime with MIT License 4 votes vote down vote up
@Override
	public boolean handle(BridgeEvent event) throws EventBridgeChainException {
		boolean isHandle = Boolean.FALSE;
		
		if(BridgeEventType.SEND == event.type()) {
			Vertx vertx = VertxHolder.getVertx();
			SockJSSocket sockJSSocket = event.socket();
			
			Map<String, Object> rawMessage = event.getRawMessage().getMap();
			
			
			String replyAddress = (String) rawMessage.get("replyAddress");
			String address = (String) rawMessage.get("address");
			
			if("vertx.basicauthmanager.login".equals(address)) {
				@SuppressWarnings("unchecked")
				Map<String, String> credential = (Map<String, String>) rawMessage.get("body");
				String userId = credential.get("username");
				//String password = credential.get("password");
				
				if(userId == null || "".equals(userId)) {
					logger.warn("Connection rejected");
					sockJSSocket.close();
					
					throw new EventBridgeChainException(true, "No user attached");
				}
				else {
					
					boolean exists = WebSocketSessionHolder.exists(userId);
					if(exists) {
						throw new EventBridgeChainException(true, "User already registered");
					}
					
					sockJSSocket.headers().set(WebSocketSessionHolder.USER_KEY, userId);
					requestLogService.logWebSocketConnection(sockJSSocket);
					
					WebSocketSessionHolder.add(userId, sockJSSocket);

					System.out.println(vertx);
					System.out.println(vertx.eventBus());
					System.out.println(userId);

					RedisSockjsClient.PUBLISH("topic/chat/user",
					         new JsonObject()
								.put("userId", userId));
					// publish there is a new user coming
//					vertx.eventBus().publish("topic/chat/user",
//				         new JsonObject()
//							.put("userId", userId));
					
					// get all online and send back to 
					JsonObject json = new JsonObject()
		                .put("type", "login") // optional
		                .put("address", replyAddress)
		                .put("body", 
	                		new JsonObject()
	                			.put("result", true)
	                			.put("list", WebSocketSessionHolder.getUsers()));
					String data = json.toString();
					
					sockJSSocket.write(Buffer.buffer(data));
					
					isHandle = Boolean.TRUE;
				}
				
			}
			
		}
		return isHandle;
	}