Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.websockets.core;
  
  import org.xnio.Buffers;
  import org.xnio.IoUtils;
  
 
 import static org.xnio.ChannelListeners.flushingChannelListener;

Author(s):
Stuart Douglas
 
 public class WebSockets {
 
     private static final Charset utf8 = Charset.forName("UTF-8");

    
Sends a complete text message, invoking the callback when complete

Parameters:
message
wsChannel
callback
 
     public static void sendText(final String messagefinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
         final ByteBuffer data = ByteBuffer.wrap(message.getBytes());
         sendInternal(new ByteBuffer[]{data}, .wsChannelcallback);
     }

    
Sends a complete text message, invoking the callback when complete

Parameters:
message
wsChannel
 
     public static void sendTextBlocking(final String messagefinal WebSocketChannel wsChannelthrows IOException {
         final ByteBuffer data = ByteBuffer.wrap(message.getBytes());
         sendBlockingInternal(new ByteBuffer[]{data}, .wsChannel);
     }

    
Sends a complete ping message, invoking the callback when complete

Parameters:
data
wsChannel
callback
 
     public static void sendPing(final ByteBuffer datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
         sendInternal(new ByteBuffer[]{data}, .wsChannelcallback);
     }

    
Sends a complete ping message, invoking the callback when complete

Parameters:
data
wsChannel
callback
 
     public static void sendPing(final ByteBuffer[] datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
         sendInternal(data.wsChannelcallback);
     }

    
Sends a complete ping message using blocking IO

Parameters:
data
wsChannel
 
     public static void sendPingBlocking(final ByteBuffer datafinal WebSocketChannel wsChannelthrows IOException {
         sendBlockingInternal(new ByteBuffer[]{data}, .wsChannel);
     }

    
Sends a complete ping message using blocking IO

Parameters:
data
wsChannel
 
     public static void sendPingBlocking(final ByteBuffer[] datafinal WebSocketChannel wsChannelthrows IOException {
         sendBlockingInternal(data.wsChannel);
     }

    
Sends a complete pong message, invoking the callback when complete

Parameters:
data
wsChannel
callback
 
     public static void sendPong(final ByteBuffer datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
         sendInternal(new ByteBuffer[]{data}, .wsChannelcallback);
     }


    
Sends a complete pong message, invoking the callback when complete

Parameters:
data
wsChannel
callback
    public static void sendPong(final ByteBuffer[] datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        sendInternal(data.wsChannelcallback);
    }

    
Sends a complete pong message using blocking IO

Parameters:
data
wsChannel
    public static void sendPongBlocking(final ByteBuffer datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(new ByteBuffer[]{data}, .wsChannel);
    }

    
Sends a complete pong message using blocking IO

Parameters:
data
wsChannel
    public static void sendPongBlocking(final ByteBuffer[] datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(data.wsChannel);
    }

    
Sends a complete text message, invoking the callback when complete

Parameters:
data
wsChannel
callback
    public static void sendBinary(final ByteBuffer datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        sendInternal(new ByteBuffer[]{data}, .wsChannelcallback);
    }

    
Sends a complete text message, invoking the callback when complete

Parameters:
data
wsChannel
callback
    public static void sendBinary(final ByteBuffer[] datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        sendInternal(data.wsChannelcallback);
    }

    
Sends a complete text message, invoking the callback when complete

Parameters:
data
wsChannel
    public static void sendBinaryBlocking(final ByteBuffer datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(new ByteBuffer[]{data}, .wsChannel);
    }

    
Sends a complete text message, invoking the callback when complete

Parameters:
data
wsChannel
    public static void sendBinaryBlocking(final ByteBuffer[] datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(data.wsChannel);
    }

    
Sends a complete close message, invoking the callback when complete

Parameters:
data
wsChannel
callback
    public static void sendClose(final ByteBuffer datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        sendInternal(new ByteBuffer[]{data}, .wsChannelcallback);
    }

    
Sends a complete close message, invoking the callback when complete

Parameters:
data
wsChannel
callback
    public static void sendClose(final ByteBuffer[] datafinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        sendInternal(data.wsChannelcallback);
    }


    
Sends a complete close message, invoking the callback when complete

Parameters:
data
wsChannel
    public static void sendCloseBlocking(final ByteBuffer datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(new ByteBuffer[]{data}, .wsChannel);
    }

    
Sends a complete close message, invoking the callback when complete

Parameters:
data
wsChannel
    public static void sendCloseBlocking(final ByteBuffer[] datafinal WebSocketChannel wsChannelthrows IOException {
        sendBlockingInternal(data.wsChannel);
    }
    private static void sendInternal(final ByteBuffer[] dataWebSocketFrameType typefinal WebSocketChannel wsChannelfinal WebSocketCallback<Voidcallback) {
        try {
            long totalData = Buffers.remaining(data);
            StreamSinkFrameChannel channel = wsChannel.send(typetotalData);
            sendData(datawsChannelcallbackchannelnull);
        } catch (IOException e) {
            if (callback != null) {
                callback.onError(wsChannelnulle);
            } else {
                IoUtils.safeClose(wsChannel);
            }
        }
    }
    private static <T> void sendData(final ByteBuffer[] datafinal WebSocketChannel wsChannelfinal WebSocketCallback<T> callbackStreamSinkFrameChannel channelfinal T contextthrows IOException {
        boolean hasRemaining = true;
        while (hasRemaining) {
            long res = channel.write(data);
            hasRemaining = Buffers.hasRemaining(data);
            if (res == 0 && hasRemaining) {
                channel.getWriteSetter().set(new ChannelListener<StreamSinkFrameChannel>() {
                    @Override
                    public void handleEvent(StreamSinkFrameChannel channel) {
                        do {
                            try {
                                long res = channel.write(data);
                                if (res == 0) {
                                    return;
                                }
                            } catch (IOException e) {
                                handleIoException(channelecallbackcontextwsChannel);
                                return;
                            }
                        } while (Buffers.hasRemaining(data));
                        channel.suspendWrites();
                        try {
                            flushChannelAsync(wsChannelcallbackchannelcontext);
                        } catch (IOException e) {
                            handleIoException(channelecallbackcontextwsChannel);
                        }
                    }
                });
                channel.resumeWrites();
                return;
            }
        }
        flushChannelAsync(wsChannelcallbackchannelcontext);
    }
    private static <T> void handleIoException(StreamSinkFrameChannel channelIOException eWebSocketCallback<T> callback, T contextWebSocketChannel wsChannel) {
        if (callback != null) {
            callback.onError(channel.getWebSocketChannel(), contexte);
        }
        IoUtils.safeClose(wsChannel);
        channel.suspendWrites();
    }
    private static <T> void flushChannelAsync(final WebSocketChannel wsChannelfinal WebSocketCallback<T> callbackStreamSinkFrameChannel channelfinal T contextthrows IOException {
        final WebSocketFrameType type = channel.getType();
        channel.shutdownWrites();
        if (!channel.flush()) {
            channel.getWriteSetter().set(flushingChannelListener(
                    new ChannelListener<StreamSinkFrameChannel>() {
                        @Override
                        public void handleEvent(StreamSinkFrameChannel channel) {
                            if (callback != null) {
                                callback.complete(wsChannelcontext);
                            }
                            if (type == . && wsChannel.isCloseFrameReceived()) {
                                IoUtils.safeClose(wsChannel);
                            }
                        }
                    }, new ChannelExceptionHandler<StreamSinkFrameChannel>() {
                        @Override
                        public void handleException(StreamSinkFrameChannel channelIOException exception) {
                            if (callback != null) {
                                callback.onError(wsChannelcontextexception);
                            }
                            if (type == . && wsChannel.isCloseFrameReceived()) {
                                IoUtils.safeClose(wsChannel);
                            }
                        }
                    }
            ));
            channel.resumeWrites();
            return;
        }
        if (callback != null) {
            callback.complete(wsChannelcontext);
        }
        if (type == . && wsChannel.isCloseFrameReceived()) {
            IoUtils.safeClose(wsChannel);
        }
    }
    private static void sendBlockingInternal(final ByteBuffer[] dataWebSocketFrameType typefinal WebSocketChannel wsChannelthrows IOException {
        long totalData = Buffers.remaining(data);
        StreamSinkFrameChannel channel = wsChannel.send(typetotalData);
        for (ByteBuffer buf : data) {
            while (buf.hasRemaining()) {
                int res = channel.write(buf);
                if (res == 0) {
                    channel.awaitWritable();
                }
            }
        }
        channel.shutdownWrites();
        while (!channel.flush()) {
            channel.awaitWritable();
        }
        if (type == . && wsChannel.isCloseFrameReceived()) {
            IoUtils.safeClose(wsChannel);
        }
    }
    private WebSockets() {
    }
    public static ByteBuffer mergeBuffers(ByteBuffer... payload) {
        int size = (int) Buffers.remaining(payload);
        if (size == 0) {
            return .;
        }
        ByteBuffer buffer = ByteBuffer.allocate(size);
        for (ByteBuffer buf : payload) {
            buffer.put(buf);
        }
        buffer.flip();
        return buffer;
    }
New to GrepCode? Check out our FAQ X