WebSocket implements WebSocketListener, WebsocketEncoder { @Override public void onWebSocketText(final String message) { if (this.listener != null) listener.onIncomingText(message); if (isAlive()) { try { uiContext.onMessageReceived(); if (monitor != null) monitor.onMessageReceived(WebSocket.this, message); final JsonObject jsonObject; try (final JsonReader reader = uiContext.getJsonProvider().createReader(new StringReader(message))) { jsonObject = reader.readObject(); } if (jsonObject.containsKey(ClientToServerModel.HEARTBEAT_REQUEST.toStringValue())) { sendHeartbeat(); } else if (jsonObject.containsKey(ClientToServerModel.TERMINAL_LATENCY.toStringValue())) { processRoundtripLatency(jsonObject); } else if (jsonObject.containsKey(ClientToServerModel.APPLICATION_INSTRUCTIONS.toStringValue())) { processInstructions(jsonObject); } else if (jsonObject.containsKey(ClientToServerModel.ERROR_MSG.toStringValue())) { processTerminalLog(jsonObject, ClientToServerModel.ERROR_MSG); } else if (jsonObject.containsKey(ClientToServerModel.WARN_MSG.toStringValue())) { processTerminalLog(jsonObject, ClientToServerModel.WARN_MSG); } else if (jsonObject.containsKey(ClientToServerModel.INFO_MSG.toStringValue())) { processTerminalLog(jsonObject, ClientToServerModel.INFO_MSG); } else { log.error("Unknown message from terminal #{} : {}", uiContext.getID(), message); } if (monitor != null) monitor.onMessageProcessed(this, message); } catch (final Throwable e) { log.error("Cannot process message from terminal #" + uiContext.getID() + " : " + message, e); } finally { if (monitor != null) monitor.onMessageUnprocessed(this, message); } } else { log.info("UI Context #{} is destroyed, message dropped from terminal : {}", uiContext != null ? uiContext.getID() : -1, message); } } WebSocket(); @Override void onWebSocketConnect(final Session session); @Override void onWebSocketError(final Throwable throwable); @Override void onWebSocketClose(final int statusCode, final String reason); @Override void onWebSocketText(final String message); @Override void onWebSocketBinary(final byte[] payload, final int offset, final int len); void sendRoundTrip(); void flush(); void close(); void disconnect(); @Override void beginObject(); @Override void endObject(); @Override void encode(final ServerToClientModel model, final Object value); ServletUpgradeRequest getRequest(); void setRequest(final ServletUpgradeRequest request); void setApplicationManager(final ApplicationManager applicationManager); void setMonitor(final WebsocketMonitor monitor); void setContext(final TxnContext context); void setListener(final Listener listener);  }
@Test public void testInstruction() { final JsonProvider provider = JsonProvider.provider(); final JsonObjectBuilder job = provider.createObjectBuilder(); final JsonArrayBuilder jab = provider.createArrayBuilder(); final JsonObjectBuilder job1 = provider.createObjectBuilder(); job1.add(ClientToServerModel.OBJECT_ID.toStringValue(), false); jab.add(job1.build()); job.add(ClientToServerModel.APPLICATION_INSTRUCTIONS.toStringValue(), jab); webSocket.onWebSocketText(job.build().toString()); }
@Test public void testOnWebSocketTextRoundTripLatency() { encodedValues.clear(); final JsonObjectBuilder job = JsonProvider.provider().createObjectBuilder(); job.add(ClientToServerModel.HEARTBEAT_REQUEST.toStringValue(), JsonValue.NULL); webSocket.onWebSocketText(job.build().toString()); assertEquals(encodedValues, List.of(new Pair<>(ServerToClientModel.HEARTBEAT, null), new Pair<>(ServerToClientModel.END, null))); }
@Test public void testErrorMessage() { final JsonObjectBuilder job = JsonProvider.provider().createObjectBuilder(); job.add(ClientToServerModel.ERROR_MSG.toStringValue(), "Test"); job.add(ClientToServerModel.OBJECT_ID.toStringValue(), 1); webSocket.onWebSocketText(job.build().toString()); }
@Test public void testWarningMessage() { final JsonObjectBuilder job = JsonProvider.provider().createObjectBuilder(); job.add(ClientToServerModel.WARN_MSG.toStringValue(), "Test"); job.add(ClientToServerModel.OBJECT_ID.toStringValue(), 1); webSocket.onWebSocketText(job.build().toString()); }
@Test public void testInfoMessage() { final JsonObjectBuilder job = JsonProvider.provider().createObjectBuilder(); job.add(ClientToServerModel.INFO_MSG.toStringValue(), "Test"); job.add(ClientToServerModel.OBJECT_ID.toStringValue(), 1); webSocket.onWebSocketText(job.build().toString()); }