java.net.http.WebSocket.Listener Java Examples
The following examples show how to use
java.net.http.WebSocket.Listener.
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: WebSocketControllerTest.java From mangooio with Apache License 2.0 | 7 votes |
@Test public void testWebSocketConnection() throws Exception { // given final HttpClient httpClient = HttpClient.newHttpClient(); final Config config = Application.getInstance(Config.class); final String uri = "ws://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/websocket"; // when Listener listener = new Listener() { @Override public void onOpen(WebSocket webSocket) { connected = true; } }; httpClient.newWebSocketBuilder().buildAsync(new URI(uri), listener).join(); // then assertThat(connected, equalTo(true)); }
Example #2
Source File: WebSocketServiceTest.java From mangooio with Apache License 2.0 | 6 votes |
@Test public void testCloseChannel() throws Exception { //given final HttpClient httpClient = HttpClient.newHttpClient(); final Config config = Application.getInstance(Config.class); final String url = "ws://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/websocket"; final WebSocketService webSocketService = Application.getInstance(WebSocketService.class); webSocketService.removeChannels("/websocket"); // when Listener listener = new Listener() { @Override public void onOpen(WebSocket webSocket) { } }; httpClient.newWebSocketBuilder().buildAsync(new URI(url), listener).join(); webSocketService.close("/websocket"); //then assertThat(webSocketService.getChannels("/websocket"), not(nullValue())); assertThat(webSocketService.getChannels("/websocket").size(), equalTo(0)); }
Example #3
Source File: HttpClientDemo.java From Learn-Java-12-Programming with MIT License | 5 votes |
@Override public CompletionStage onText(WebSocket webSocket, CharSequence data, boolean last) { System.out.println("Method onText() got data: " + data); if(!webSocket.isOutputClosed()) { webSocket.sendText("Another message", true); } return Listener.super.onText(webSocket, data, last); }
Example #4
Source File: WebSocketServiceTest.java From mangooio with Apache License 2.0 | 5 votes |
@Test public void testSendData() throws Exception { // given final HttpClient httpClient = HttpClient.newHttpClient(); final Config config = Application.getInstance(Config.class); final String url = "ws://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/websocket"; final String data = UUID.randomUUID().toString(); eventData = null; // when Listener listener = new Listener() { @Override public CompletionStage<?> onText(WebSocket webSocket, CharSequence message, boolean last) { eventData = message.toString(); return null; } }; httpClient.newWebSocketBuilder().buildAsync(new URI(url), listener).get(); Application.getInstance(WebSocketService.class).getChannels("/websocket").forEach(channel -> { try { if (channel.isOpen()) { WebSockets.sendTextBlocking(data, channel); } } catch (final IOException e) { e.printStackTrace(); } }); //then await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, equalTo(data))); }
Example #5
Source File: WebSocketServiceTest.java From mangooio with Apache License 2.0 | 5 votes |
@Test public void testSendDataWithInvalidAuthentication() throws Exception { // given final HttpClient httpClient = HttpClient.newHttpClient(); final Config config = Application.getInstance(Config.class); final String url = "ws://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/websocketauth"; final String data = UUID.randomUUID().toString(); eventData = null; PasetoV1LocalBuilder token = Pasetos.V1.LOCAL.builder().setSubject("foo") .setExpiration(LocalDateTime.now().plusHours(1).atZone(ZoneId.systemDefault()).toInstant()) .claim(ClaimKey.TWO_FACTOR.toString(), "false") .setSharedSecret(new SecretKeySpec("oskdlwsodkcmansjdkwsowekd5jfvsq2mckdkalsodkskajsfdsfdsfvvkdkcskdsqidsjk".getBytes(StandardCharsets.UTF_8), "AES")); Listener listener = new Listener() { @Override public CompletionStage<?> onText(WebSocket webSocket, CharSequence message, boolean last) { eventData = message.toString(); return null; } }; httpClient.newWebSocketBuilder() .header("Cookie", config.getAuthenticationCookieName() + "=" + token.compact()) .buildAsync(new URI(url), listener); await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, not(equalTo(data)))); Application.getInstance(WebSocketService.class).getChannels("/websocketauth").forEach(channel -> { try { if (channel.isOpen()) { WebSockets.sendTextBlocking(data, channel); } } catch (final IOException e) { e.printStackTrace(); } }); // then await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, not(equalTo(data)))); }
Example #6
Source File: HttpClientDemo.java From Learn-Java-12-Programming with MIT License | 4 votes |
@Override public void onOpen(WebSocket webSocket) { System.out.println("Connection established."); webSocket.sendText("Some message", true); Listener.super.onOpen(webSocket); }
Example #7
Source File: HttpClientDemo.java From Learn-Java-12-Programming with MIT License | 4 votes |
@Override public CompletionStage onClose(WebSocket webSocket, int statusCode, String reason) { System.out.println("Closed with status " + statusCode + ", reason: " + reason); return Listener.super.onClose(webSocket, statusCode, reason); }
Example #8
Source File: WebSocketServiceTest.java From mangooio with Apache License 2.0 | 4 votes |
@Test public void testSendDataWithValidAuthentication() throws Exception { // given final HttpClient httpClient = HttpClient.newHttpClient(); final Config config = Application.getInstance(Config.class); final String url = "ws://" + config.getConnectorHttpHost() + ":" + config.getConnectorHttpPort() + "/websocketauth"; final String data = UUID.randomUUID().toString(); eventData = null; // then PasetoV1LocalBuilder token = Pasetos.V1.LOCAL.builder().setSubject("foo") .setExpiration(LocalDateTime.now().plusHours(1).atZone(ZoneId.systemDefault()).toInstant()) .claim(ClaimKey.TWO_FACTOR.toString(), "false") .setSharedSecret(new SecretKeySpec(config.getAuthenticationCookieSecret().getBytes(StandardCharsets.UTF_8), "AES")); Listener listener = new Listener() { @Override public CompletionStage<?> onText(WebSocket webSocket, CharSequence message, boolean last) { eventData = message.toString(); return null; } }; httpClient.newWebSocketBuilder() .header("Cookie", config.getAuthenticationCookieName() + "=" + token.compact()) .buildAsync(new URI(url), listener); await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, not(equalTo(data)))); Application.getInstance(WebSocketService.class).getChannels("/websocketauth").forEach(channel -> { try { if (channel.isOpen()) { WebSockets.sendTextBlocking(data, channel); } } catch (final IOException e) { e.printStackTrace(); } }); // then await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(eventData, equalTo(data))); }