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

The following examples show how to use org.springframework.web.reactive.socket.CloseStatus. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: UndertowWebSocketSession.java    License: 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
/** 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 3
@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 4
Source Project: spring-analysis-note   Source File: StandardWebSocketSession.java    License: 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 5
@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 6
Source Project: java-technology-stack   Source File: UndertowWebSocketSession.java    License: 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 7
/** 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
@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 9
Source Project: java-technology-stack   Source File: StandardWebSocketSession.java    License: 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 10
@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 11
Source Project: vertx-spring-boot   Source File: VertxWebSocketSession.java    License: 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 12
@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 13
@Override
public Mono<Void> close(CloseStatus status) {
	return getDelegate().getOutbound().sendClose(status.getCode(), status.getReason());
}
 
Example 14
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
	if (this.delegateSession != null) {
		this.delegateSession.handleClose(new CloseStatus(statusCode, reason));
	}
}
 
Example 15
Source Project: spring-analysis-note   Source File: JettyWebSocketSession.java    License: 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 16
@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 17
@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 18
@Override
public Mono<Void> close(CloseStatus status) {
	return getDelegate().getOutbound().sendClose(status.getCode(), status.getReason());
}
 
Example 19
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
	if (this.delegateSession != null) {
		this.delegateSession.handleClose(new CloseStatus(statusCode, reason));
	}
}
 
Example 20
Source Project: java-technology-stack   Source File: JettyWebSocketSession.java    License: 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 21
@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 22
@Override
public Mono<Void> handle(WebSocketSession session) {
	return Flux.never().mergeWith(session.close(CloseStatus.GOING_AWAY)).then();
}