Java Code Examples for io.vertx.ext.web.handler.sockjs.SockJSSocket#write()

The following examples show how to use io.vertx.ext.web.handler.sockjs.SockJSSocket#write() . 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: 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 2
Source File: TestInjectedSock.java    From nubes with Apache License 2.0 4 votes vote down vote up
@OnMessage
public void getDog(Buffer msg, SockJSSocket sock) {
	sock.write(Buffer.buffer(snoop.getName()));
}
 
Example 3
Source File: TestSockJSController.java    From nubes with Apache License 2.0 4 votes vote down vote up
@OnMessage
public void messageHandler(SockJSSocket socket, Buffer buff) {
	socket.write(buff);
}