Java Code Examples for io.undertow.websockets.core.WebSockets#sendClose()

The following examples show how to use io.undertow.websockets.core.WebSockets#sendClose() . 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: WebSocketMessageListener.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage textMessage) {
    var wrapper = (ChannelImpl) channel.getAttribute(WebSocketHandler.CHANNEL_KEY);
    ActionLog actionLog = logManager.begin("=== ws message handling begin ===");
    try {
        actionLog.action(wrapper.action);
        linkContext(channel, wrapper, actionLog);

        String data = textMessage.getData();
        logger.debug("[channel] message={}", data);     // not mask, assume ws message not containing sensitive info, the data can be json or plain text
        actionLog.track("ws", 0, 1, 0);

        validateRate(wrapper);

        Object message = wrapper.handler.fromClientMessage(data);
        wrapper.handler.listener.onMessage(wrapper, message);
    } catch (Throwable e) {
        logManager.logError(e);
        WebSockets.sendClose(closeCode(e), e.getMessage(), channel, ChannelCallback.INSTANCE);
    } finally {
        logManager.end("=== ws message handling end ===");
    }
}
 
Example 2
Source File: Term.java    From termd with Apache License 2.0 6 votes vote down vote up
HttpHandler webSocketStatusUpdateHandler() {
  WebSocketConnectionCallback webSocketConnectionCallback = (exchange, webSocketChannel) -> {
    Consumer<TaskStatusUpdateEvent> statusUpdateListener = event -> {
      Map<String, Object> statusUpdate = new HashMap<>();
      statusUpdate.put("action", "status-update");
      statusUpdate.put("event", event);

      ObjectMapper objectMapper = new ObjectMapper();
      try {
        String message = objectMapper.writeValueAsString(statusUpdate);
        WebSockets.sendText(message, webSocketChannel, null);
      } catch (JsonProcessingException e) {
        log.error("Cannot write object to JSON", e);
        String errorMessage = "Cannot write object to JSON: " + e.getMessage();
        WebSockets.sendClose(CloseMessage.UNEXPECTED_ERROR, errorMessage, webSocketChannel, null);
      }
    };
    log.debug("Registering new status update listener {}.", statusUpdateListener);
    addStatusUpdateListener(statusUpdateListener);
    webSocketChannel.addCloseTask((task) -> removeStatusUpdateListener(statusUpdateListener));
  };

  return new WebSocketProtocolHandshakeHandler(webSocketConnectionCallback);
}
 
Example 3
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 4
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 5
Source File: ChannelImpl.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    var watch = new StopWatch();
    try {
        WebSockets.sendClose(WebSocketCloseCodes.NORMAL_CLOSURE, null, channel, ChannelCallback.INSTANCE);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("ws", elapsed, 0, 1);
        LOGGER.debug("close ws channel, id={}, elapsed={}", id, elapsed);
    }
}
 
Example 6
Source File: WebSocketHandler.java    From core-ng-project with Apache License 2.0 4 votes vote down vote up
public void shutdown() {
    for (var channel : channels) {
        WebSockets.sendClose(WebSocketCloseCodes.SERVICE_RESTART, "server is shutting down", channel, ChannelCallback.INSTANCE);
    }
}
 
Example 7
Source File: UndertowWebSocketConnection.java    From pippo with Apache License 2.0 4 votes vote down vote up
@Override
public void close(int code, String reason) {
    WebSockets.sendClose(code, reason, channel, null);
}