public void waitForMessage() throws IOException { if (!connectIfNot()) return; while (!message.isComplete()) { if (!onFrame(readFrame())) break; } if (!message.isComplete()) return; try { onMessage(message); } catch (Exception e) { onError(e); } message = new Message(); }
@Override public void run() throws IOException { while (true) { waitForMessage(); if (isClosed()) { if (reconnectOnClosePause >= 0) { pause(reconnectOnClosePause); } else { break; } } } } });
public boolean connectIfNot() throws IOException { while (!connected) try { handshake(request); } catch (IOException e) { connected = false; try { onError(e); onClose(); } catch (Exception ex) { onError(ex); } if (reconnectOnErrorPause >= 0) { pause(reconnectOnErrorPause); } else { break; } } return connected; }
@Test public void echoTest() throws IOException, URISyntaxException, InterruptedException { handler = new WebSocketHandler() { @Override public void onMessage(WebSocketListener listener, Message message) { listener.sendMessage(message); } }; AtomicReference<String> messageHolder = new AtomicReference<>(); SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()) { @Override public void onMessage(Message message) { messageHolder.set(message.asString()); } }; client.send("foo bar"); client.waitForMessage(); Assert.assertEquals("foo bar", messageHolder.get()); }
SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()); client.send("close"); try { client.waitForMessage(); Assert.assertTrue(client.isClosed()); } catch (SocketException e) { Assert.assertEquals("Connection reset", e.getMessage()); client = new SimpleWebSocketClient("ws://localhost:" + getPort()); client.connectIfNot(); client.close(); Thread.sleep(10); Assert.assertTrue(client.isClosed()); Assert.assertEquals("closed", messageHolder.get());
protected void doWithReconnects(IORunnable runnable) { while (true) { try { if (!connectIfNot()) return; runnable.run(); break; } catch (IOException e) { connected = false; try { onError(e); onClose(); } catch (Exception ex) { onError(ex); } if (reconnectOnErrorPause >= 0) { pause(reconnectOnErrorPause); } else { break; } } } }
public void send(byte[] data) throws IOException { send(data, 0, data.length); }
@Test public void closeTest_2() throws IOException, URISyntaxException, InterruptedException { AtomicReference<String> messageHolder = new AtomicReference<>(); handler = new WebSocketHandler() { @Override public void onDisconnect(WebSocketListener listener) { Assert.assertTrue(messageHolder.compareAndSet(null, "closed")); } }; SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()); client.connectIfNot(); client.in.close(); Thread.sleep(10); Assert.assertEquals("closed", messageHolder.get()); }
@Test public void pingTest() throws IOException, URISyntaxException, InterruptedException { handler = new WebSocketHandler(); SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()); Assert.assertTrue(client.ping() >= 0); }
public void send(final Message message) throws IOException { doWithReconnects(new IORunnable() { @Override public void run() throws IOException { for (Frame frame : message.getFrames()) { frame.mask().write(out); } out.flush(); } }); }
protected boolean onFrame(Frame frame) { if (frame.isPing()) return true; if (frame.isClose()) { connected = false; onClose(); return false; } message.add(frame); return true; }
@Test public void limitTest() throws IOException, URISyntaxException, InterruptedException { handler = new WebSocketHandler() { @Override public void onMessage(WebSocketListener listener, Message message) { listener.sendMessage(message); } }; SimpleWebSocketClient client = new SimpleWebSocketClient("ws://localhost:" + getPort()) { @Override public void onMessage(Message message) { throw new IllegalArgumentException(); } }; byte[] data; data = new byte[1024 * 1024]; ThreadLocalRandom.current().nextBytes(data); try { client.send(data); client.waitForMessage(); Assert.assertTrue(false); } catch (SocketException e) { Assert.assertEquals("Connection reset", e.getMessage()); } }
public void send(String s) throws IOException { send(s.getBytes()); }
public void send(final Frame frame) throws IOException { doWithReconnects(new IORunnable() { @Override public void run() throws IOException { frame.write(out); if (frame.isFinalFrame()) out.flush(); } }); }
byte[] data = new byte[126]; ThreadLocalRandom.current().nextBytes(data); client.send(data); client.waitForMessage(); Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get()); client.send(data); client.waitForMessage(); Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get()); client.send(data); client.waitForMessage(); Assert.assertEquals(MD5.create().update(data).asString(), md5Holder.get());
public void send(byte[] data, int offset, int length) throws IOException { send(new Frame(data, offset, length).mask()); }
@Override public void run() { doWithReconnects(new IORunnable() { @Override public void run() throws IOException { while (true) { waitForMessage(); if (isClosed()) { if (reconnectOnClosePause >= 0) { pause(reconnectOnClosePause); } else { break; } } } } }); }