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

The following examples show how to use io.undertow.websockets.core.WebSockets#sendText() . 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 6 votes vote down vote up
@Override
protected boolean sendMessage(WebSocketMessage message) throws IOException {
	ByteBuffer buffer = message.getPayload().asByteBuffer();
	if (WebSocketMessage.Type.TEXT.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		String text = new String(buffer.array(), StandardCharsets.UTF_8);
		WebSockets.sendText(text, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.BINARY.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendBinary(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.PING.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendPing(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.PONG.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendPong(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else {
		throw new IllegalArgumentException("Unexpected message type: " + message.getType());
	}
	return true;
}
 
Example 2
Source File: UndertowWebSocketSession.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected boolean sendMessage(WebSocketMessage message) throws IOException {
	ByteBuffer buffer = message.getPayload().asByteBuffer();
	if (WebSocketMessage.Type.TEXT.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		String text = new String(buffer.array(), StandardCharsets.UTF_8);
		WebSockets.sendText(text, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.BINARY.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendBinary(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.PING.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendPing(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else if (WebSocketMessage.Type.PONG.equals(message.getType())) {
		getSendProcessor().setReadyToSend(false);
		WebSockets.sendPong(buffer, getDelegate(), new SendProcessorCallback(message.getPayload()));
	}
	else {
		throw new IllegalArgumentException("Unexpected message type: " + message.getType());
	}
	return true;
}
 
Example 3
Source File: ChannelImpl.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
public <T> void send(T message) {
    var watch = new StopWatch();
    String text = handler.toServerMessage(message);

    // refer to io.undertow.websockets.core.WebSocketChannel.send(WebSocketFrameType),
    // in concurrent env, one thread can still get hold of channel from context right before channel close listener removes it from context
    // this is to reduce chance of triggering WebSocketMessages.MESSAGES.channelClosed() exception
    // but in theory, there is still small possibility to cause channelClosed()
    if (channel.isCloseFrameSent() || channel.isCloseFrameReceived()) return;

    try {
        WebSockets.sendText(text, channel, ChannelCallback.INSTANCE);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("ws", elapsed, 0, 1);
        LOGGER.debug("send ws message, id={}, text={}, elapsed={}", id, text, elapsed);     // not mask, assume ws message not containing sensitive info, the text can be json or plain text
    }
}
 
Example 4
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 5
Source File: WebSocketUndertowServletResponse.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void sendError(int sc) throws IOException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "sendError{0}", sc);
    }
    responseHeaders.put(WebSocketUtils.SC_KEY, Integer.toString(sc));
    byte[] data = WebSocketUtils.buildResponse(responseHeaders, null, 0, 0);
    WebSockets.sendText(ByteBuffer.wrap(data), channel, null);
}
 
Example 6
Source File: WebSocketUndertowServletResponse.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void sendError(int sc, String msg) throws IOException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "sendError({0}, {1})", new Object[]{sc, msg});
    }
    responseHeaders.put(WebSocketUtils.SC_KEY, Integer.toString(sc));
    byte[] data = WebSocketUtils.buildResponse(responseHeaders, null, 0, 0);
    WebSockets.sendText(ByteBuffer.wrap(data), channel, null);
}
 
Example 7
Source File: UndertowWebSocketConnection.java    From pippo with Apache License 2.0 5 votes vote down vote up
@Override
public WebSocketConnection sendMessage(String message) throws IOException {
    checkClosed();
    WebSockets.sendText(message, channel, null);

    return this;
}
 
Example 8
Source File: SocketServer.java    From tutorials with MIT License 5 votes vote down vote up
private static AbstractReceiveListener getListener() {
    return new AbstractReceiveListener() {
        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
            final String messageData = message.getData();
            for (WebSocketChannel session : channel.getPeerConnections()) {
                WebSockets.sendText(messageData, session, null);
            }
        }
    };
}
 
Example 9
Source File: EventBusToWebSocket.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private void send(WebSocketChannel channel, String type, String data) {
    WebSockets.sendText(EventMessage.of(type, data).toJson(), channel, null);
}
 
Example 10
Source File: EventsPath.java    From PYX-Reloaded with Apache License 2.0 4 votes vote down vote up
private static void sendConnectionError(WebSocketHttpExchange exchange, WebSocketChannel channel, JsonWrapper error) {
    WebSockets.sendText(error.toString(), channel, null);
    exchange.endExchange();
}
 
Example 11
Source File: UndertowWebSocketConnection.java    From actframework with Apache License 2.0 4 votes vote down vote up
@Override
public void send(String message) {
    WebSockets.sendText(message, channel, null);
}