org.springframework.web.reactive.socket.CloseStatus Java Examples

The following examples show how to use org.springframework.web.reactive.socket.CloseStatus. 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: UndertowWebSocketSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	CloseMessage cm = new CloseMessage(status.getCode(), status.getReason());
	if (!getDelegate().isCloseFrameSent()) {
		WebSockets.sendClose(cm, getDelegate(), null);
	}
	return Mono.empty();
}
 
Example #2
Source File: VertxWebSocketSessionTest.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldClose() {
    given(mockServerWebSocket.closeHandler(any())).will(invocation -> {
        Handler<Void> handler = invocation.getArgument(0);
        handler.handle(null);
        return mockServerWebSocket;
    });

    session.close(new CloseStatus(1000, "test")).block();

    verify(mockServerWebSocket).close((short) 1000, "test");
}
 
Example #3
Source File: VertxWebSocketSession.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
    logger.debug("{}Closing web socket with status '{}'", getLogPrefix(), status);
    return Mono.create(sink -> getDelegate()
        .closeHandler(e -> {
            logger.debug("{}Web socket closed", getLogPrefix());
            sink.success();
        })
        .close((short) status.getCode(), status.getReason()));
}
 
Example #4
Source File: StandardWebSocketHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void onClose(Session session, CloseReason reason) {
	if (this.delegateSession != null) {
		int code = reason.getCloseCode().getCode();
		this.delegateSession.handleClose(new CloseStatus(code, reason.getReasonPhrase()));
	}
}
 
Example #5
Source File: StandardWebSocketSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	try {
		CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode());
		getDelegate().close(new CloseReason(code, status.getReason()));
	}
	catch (IOException ex) {
		return Mono.error(ex);
	}
	return Mono.empty();
}
 
Example #6
Source File: AbstractListenerWebSocketSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void onError(Throwable ex) {
	if (this.completionMono != null) {
		this.completionMono.onError(ex);
	}
	int code = CloseStatus.SERVER_ERROR.getCode();
	close(new CloseStatus(code, ex.getMessage()));
}
 
Example #7
Source File: AbstractListenerWebSocketSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
/** Handle a close callback from the WebSocketHandler adapter. */
void handleClose(CloseStatus reason) {
	this.receivePublisher.onAllDataRead();
	WebSocketSendProcessor sendProcessor = this.sendProcessor;
	if (sendProcessor != null) {
		sendProcessor.cancel();
		sendProcessor.onComplete();
	}
}
 
Example #8
Source File: UndertowWebSocketSession.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	CloseMessage cm = new CloseMessage(status.getCode(), status.getReason());
	if (!getDelegate().isCloseFrameSent()) {
		WebSockets.sendClose(cm, getDelegate(), null);
	}
	return Mono.empty();
}
 
Example #9
Source File: StandardWebSocketHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void onClose(Session session, CloseReason reason) {
	if (this.delegateSession != null) {
		int code = reason.getCloseCode().getCode();
		this.delegateSession.handleClose(new CloseStatus(code, reason.getReasonPhrase()));
	}
}
 
Example #10
Source File: StandardWebSocketSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	try {
		CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode());
		getDelegate().close(new CloseReason(code, status.getReason()));
	}
	catch (IOException ex) {
		return Mono.error(ex);
	}
	return Mono.empty();
}
 
Example #11
Source File: AbstractListenerWebSocketSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void onError(Throwable ex) {
	if (this.completionMono != null) {
		this.completionMono.onError(ex);
	}
	int code = CloseStatus.SERVER_ERROR.getCode();
	close(new CloseStatus(code, ex.getMessage()));
}
 
Example #12
Source File: AbstractListenerWebSocketSession.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** Handle a close callback from the WebSocketHandler adapter. */
void handleClose(CloseStatus reason) {
	this.receivePublisher.onAllDataRead();
	WebSocketSendProcessor sendProcessor = this.sendProcessor;
	if (sendProcessor != null) {
		sendProcessor.cancel();
		sendProcessor.onComplete();
	}
}
 
Example #13
Source File: UndertowWebSocketHandlerAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) {
	CloseMessage closeMessage = new CloseMessage(message.getData().getResource());
	this.session.handleClose(new CloseStatus(closeMessage.getCode(), closeMessage.getReason()));
	message.getData().free();
}
 
Example #14
Source File: WebSocketMessagingHandler.java    From jetlinks-community with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public Mono<Void> handle(@Nonnull WebSocketSession session) {
    String[] path = session.getHandshakeInfo().getUri().getPath().split("[/]");
    if (path.length == 0) {
        return session.send(Mono.just(session.textMessage(JSON.toJSONString(
            Message.error("auth", null, "错误的请求")
        )))).then(session.close(CloseStatus.BAD_DATA));
    }
    String token = path[path.length - 1];

    Map<String, Disposable> subs = new ConcurrentHashMap<>();

    return userTokenManager.getByToken(token)
        .map(UserToken::getUserId)
        .flatMap(authenticationManager::getByUserId)
        .switchIfEmpty(session
            .send(Mono.just(session.textMessage(JSON.toJSONString(
                Message.authError()
            ))))
            .then(session.close(CloseStatus.BAD_DATA))
            .then(Mono.empty()))
        .flatMap(auth -> session
            .receive()
            .doOnNext(message -> {
                try {
                    MessagingRequest request = JSON.parseObject(message.getPayloadAsText(), MessagingRequest.class);
                    if (StringUtils.isEmpty(request.getId())) {
                        session
                            .send(Mono.just(session.textMessage(JSON.toJSONString(
                                Message.error(request.getType().name(), null, "id不能为空")
                            )))).subscribe();
                    }
                    if (request.getType() == MessagingRequest.Type.sub) {
                        //重复订阅
                        if (subs.containsKey(request.getId())) {
                            return;
                        }
                        subs.put(request.getId(), messagingManager
                            .subscribe(SubscribeRequest.of(request, auth))
                            .onErrorResume(err -> Mono.just(Message.error(request.getId(), request.getTopic(), err.getMessage())))
                            .map(msg -> session.textMessage(JSON.toJSONString(msg)))
                            .doOnComplete(() -> {
                                subs.remove(request.getId());
                                Mono.just(session.textMessage(JSON.toJSONString(Message.complete(request.getId()))))
                                    .as(session::send)
                                    .subscribe();
                            })
                            .flatMap(msg -> session.send(Mono.just(msg)))
                            .subscribe()
                        );

                    } else if (request.getType() == MessagingRequest.Type.unsub) {
                        Optional.ofNullable(subs.remove(request.getId()))
                            .ifPresent(Disposable::dispose);
                    } else {
                        session.send(Mono.just(session.textMessage(JSON.toJSONString(
                            Message.error(request.getId(), request.getTopic(), "不支持的类型:" + request.getType())
                        )))).subscribe();
                    }
                } catch (Exception e) {
                    log.warn(e.getMessage(),e);
                    session.send(Mono.just(session.textMessage(JSON.toJSONString(
                        Message.error("illegal_argument", null, "消息格式错误")
                    )))).subscribe();
                }
            })
            .then())
        .doFinally(r -> {
            subs.values().forEach(Disposable::dispose);
            subs.clear();
        });

}
 
Example #15
Source File: ReactorNettyWebSocketSession.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	return getDelegate().getOutbound().sendClose(status.getCode(), status.getReason());
}
 
Example #16
Source File: JettyWebSocketSession.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	getDelegate().close(status.getCode(), status.getReason());
	return Mono.empty();
}
 
Example #17
Source File: JettyWebSocketHandlerAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
	if (this.delegateSession != null) {
		this.delegateSession.handleClose(new CloseStatus(statusCode, reason));
	}
}
 
Example #18
Source File: JettyWebSocketSession.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	getDelegate().close(status.getCode(), status.getReason());
	return Mono.empty();
}
 
Example #19
Source File: UndertowWebSocketHandlerAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) {
	CloseMessage closeMessage = new CloseMessage(message.getData().getResource());
	this.session.handleClose(new CloseStatus(closeMessage.getCode(), closeMessage.getReason()));
	message.getData().free();
}
 
Example #20
Source File: JettyWebSocketHandlerAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
	if (this.delegateSession != null) {
		this.delegateSession.handleClose(new CloseStatus(statusCode, reason));
	}
}
 
Example #21
Source File: ReactorNettyWebSocketSession.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> close(CloseStatus status) {
	return getDelegate().getOutbound().sendClose(status.getCode(), status.getReason());
}
 
Example #22
Source File: WebSocketIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
	return Flux.never().mergeWith(session.close(CloseStatus.GOING_AWAY)).then();
}