io.vertx.ext.web.handler.sockjs.BridgeEvent Java Examples

The following examples show how to use io.vertx.ext.web.handler.sockjs.BridgeEvent. 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: EventBridgeChain.java    From slime with MIT License 6 votes vote down vote up
public EventBridgeChainResponse processInChain(BridgeEvent event) {
	EventBridgeChainResponse response = new EventBridgeChainResponse();
	
	for (EventBridgeChainHandler handler : handlers) {
		boolean handle = Boolean.FALSE;
		try {
			handle = handler.handle(event);
			if(handle) {
				response.setProcesssed(Boolean.TRUE);
				response.setHandler(handler);
				break;
			}
		} catch (EventBridgeChainException e) {
			handle = e.isProccessed();
			if(handle) {
				response.setHandler(handler);
				response.setException(e);
				response.setProcesssed(Boolean.TRUE);
				break;
			}
		}
	}
	
	
	return response;
}
 
Example #2
Source File: RedisSockjsClient.java    From slime with MIT License 5 votes vote down vote up
public static void REGISTER(BridgeEvent event) {
	Jedis jedis = null;
	try {
		JsonObject rawMessage = event.getRawMessage();
		String address = rawMessage.getString("address");
		jedis = JedisConnectionPool.getJedisConnection(10);
		jedis.zincrby(RedisKeyStore.REGIST + ":CHANNELS:" + serverKey, -1, address);
		jedis.zincrby(RedisKeyStore.REGIST + ":SERVERS:" + address, -1, serverKey);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (jedis != null)
			JedisConnectionPool.close(jedis);
	}
}
 
Example #3
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 #4
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 #5
Source File: BridgeEventImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public BridgeEvent setRawMessage(JsonObject message) {
  if (message != rawMessage) {
    rawMessage.clear().mergeIn(message);
  }
  return this;
}
 
Example #6
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;
	}
 
Example #7
Source File: PubSubSockJsServer.java    From slime with MIT License 4 votes vote down vote up
@Override
  public void start() throws Exception{

	if( VertxHolder.getVertx() == null ) VertxHolder.setVertx(vertx);
	logger.info("Pub/Sub SockJs Server started on port {}", serverPort);
	
	Router router = Router.router(vertx);
	SockJSHandler socketHandler = SockJSHandler.create(vertx)
		.bridge(new BridgeOptions()
				.addOutboundPermitted(new PermittedOptions().setAddressRegex("^topic/.*"))
				
				// use to set client ping timeout -> will result closing connection if no ping sent
				/*.setPingTimeout(60000)*/,
				new Handler<BridgeEvent>() {
					@Override
					public void handle(BridgeEvent event) {
						boolean isResult = true;
						String message = "Oops!";
						
						System.out.println(event.type());
						
						EventBridgeChainResponse processInChain = eventBridgeChain.processInChain(event);
						if(processInChain.isProcesssed()) {
							if(processInChain.getException() != null) {
								isResult = false;
								logger.error(processInChain.getException().getMessage(), 
										processInChain.getException());
								message = processInChain.getException().getMessage();
							}
						}
						
					
						
						if(isResult) {
							
							if(event.type().equals(BridgeEventType.REGISTER)){
								System.out.println(event.getRawMessage());
								RedisSockjsClient.REGISTER(event);
							}
							
							
							event.complete(isResult);
						}
						else {
							event.fail(new Exception(message));
						}
						
					}
				});
	
	
	router.route("/sockjs/*").handler(
    	socketHandler
	);

	/*
	 * subscribe published message
	 */
	vertx.deployVerticle(new MessageSubscribeVerticle(), new DeploymentOptions().setWorker(true).setMultiThreaded(true));
	vertx.deployVerticle(new MessageSubscribeVerticle(), new DeploymentOptions().setWorker(true).setMultiThreaded(true));
	vertx.deployVerticle(new MessageSubscribeVerticle(), new DeploymentOptions().setWorker(true).setMultiThreaded(true));

    HttpServer server = vertx.createHttpServer().requestHandler(router::accept);
    
    
    server.listen(serverPort);

}
 
Example #8
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@SOCKET_CREATED
public void onSocketCreated(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #9
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@SOCKET_CLOSED
public void onSocketClosed(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #10
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@SEND
public void onClientMsgSent(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #11
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@PUBLISH
public void onPublish(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #12
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@RECEIVE
public void onReceive(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #13
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@REGISTER
public void onRegister(BridgeEvent be, EventBus eb) {
	forward(be, eb);
}
 
Example #14
Source File: EBBridgeDefaultController.java    From nubes with Apache License 2.0 4 votes vote down vote up
private static void forward(BridgeEvent be, EventBus eb) {
	String type = be.type().toString();
	eb.send(type, type);
	be.complete(true);
}
 
Example #15
Source File: EventBridgeChainHandler.java    From slime with MIT License votes vote down vote up
boolean handle(BridgeEvent event) throws EventBridgeChainException;