Search in sources :

Example 11 with WebSocket

use of com.neovisionaries.ws.client.WebSocket in project FlareBot by FlareBot.

the class WebSocketFactory method createSocket.

@Override
public WebSocket createSocket(URI uri) throws IOException {
    WebSocket socket = super.createSocket(uri);
    socket.addListener(this.listener);
    return socket;
}
Also used : WebSocket(com.neovisionaries.ws.client.WebSocket)

Example 12 with WebSocket

use of com.neovisionaries.ws.client.WebSocket in project FlareBot by FlareBot.

the class WebSocketFactory method createSocket.

@Override
public WebSocket createSocket(URI uri, int timeout) throws IOException {
    WebSocket socket = super.createSocket(uri, timeout);
    socket.addListener(this.listener);
    return socket;
}
Also used : WebSocket(com.neovisionaries.ws.client.WebSocket)

Example 13 with WebSocket

use of com.neovisionaries.ws.client.WebSocket in project ninja by ninjaframework.

the class ChatWebSocketTest method sendAndReceiveBinary.

@Test
public void sendAndReceiveBinary() throws IOException, WebSocketException, InterruptedException {
    String chatId = UUID.randomUUID().toString();
    String url = withBaseWebSocketUrl("/chat");
    WebSocket ws = new WebSocketFactory().createSocket(url);
    try {
        ws.addHeader("X-Chat-Id", chatId);
        ws.connect();
        BlockingQueue<byte[]> received = new LinkedBlockingQueue<>();
        ws.addListener(new WebSocketAdapter() {

            @Override
            public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
                received.put(binary);
            }
        });
        ws.sendText("binary1");
        byte[] reply = received.poll(2L, TimeUnit.SECONDS);
        assertThat(reply, is(ChatWebSocket.BINARY1));
    } finally {
        ws.disconnect(WebSocketCloseCode.NORMAL);
    }
}
Also used : WebSocketAdapter(com.neovisionaries.ws.client.WebSocketAdapter) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) OpeningHandshakeException(com.neovisionaries.ws.client.OpeningHandshakeException) IOException(java.io.IOException) WebSocketException(com.neovisionaries.ws.client.WebSocketException) WebSocket(com.neovisionaries.ws.client.WebSocket) Test(org.junit.Test)

Example 14 with WebSocket

use of com.neovisionaries.ws.client.WebSocket in project ninja by ninjaframework.

the class ChatWebSocketTest method unsupportedProtocol.

@Test
public void unsupportedProtocol() throws IOException, WebSocketException {
    String url = withBaseWebSocketUrl("/chat");
    WebSocket ws = new WebSocketFactory().createSocket(url);
    try {
        ws.addProtocol("chat2");
        ws.connect();
        assertThat(ws.getAgreedProtocol(), is(nullValue()));
    } finally {
        ws.disconnect(WebSocketCloseCode.NORMAL);
    }
}
Also used : WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) WebSocket(com.neovisionaries.ws.client.WebSocket) Test(org.junit.Test)

Example 15 with WebSocket

use of com.neovisionaries.ws.client.WebSocket in project ninja by ninjaframework.

the class ChatWebSocketTest method handshakeUnauthorized.

@Test
public void handshakeUnauthorized() throws IOException, WebSocketException {
    String url = withBaseWebSocketUrl("/chat?status=401");
    WebSocket ws = new WebSocketFactory().createSocket(url);
    try {
        ws.connect();
        fail("should have failed with a 401");
    } catch (OpeningHandshakeException e) {
        assertThat(e.getStatusLine().getStatusCode(), is(401));
    } finally {
        ws.disconnect(WebSocketCloseCode.NORMAL);
    }
}
Also used : OpeningHandshakeException(com.neovisionaries.ws.client.OpeningHandshakeException) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) WebSocket(com.neovisionaries.ws.client.WebSocket) Test(org.junit.Test)

Aggregations

WebSocket (com.neovisionaries.ws.client.WebSocket)17 WebSocketFactory (com.neovisionaries.ws.client.WebSocketFactory)10 WebSocketAdapter (com.neovisionaries.ws.client.WebSocketAdapter)6 Test (org.junit.Test)6 WebSocketException (com.neovisionaries.ws.client.WebSocketException)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 OpeningHandshakeException (com.neovisionaries.ws.client.OpeningHandshakeException)3 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)3 IOException (java.io.IOException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)2 ProxySettings (com.neovisionaries.ws.client.ProxySettings)2 WebSocketListener (com.neovisionaries.ws.client.WebSocketListener)2 InetSocketAddress (java.net.InetSocketAddress)2 PasswordAuthentication (java.net.PasswordAuthentication)2 Proxy (java.net.Proxy)2 ProxySelector (java.net.ProxySelector)2 SocketAddress (java.net.SocketAddress)2