List of usage examples for javax.websocket MessageHandler.Whole MessageHandler.Whole
MessageHandler.Whole
From source file:io.kodokojo.bdd.stage.AccessRestWhen.java
private void connectToWebSocket(UserInfo requesterUserInfo, boolean expectSuccess) { try {/* ww w. j a v a 2s . c o m*/ final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build(); ClientManager client = ClientManager.createClient(); if (requesterUserInfo != null) { client.getProperties().put(ClientProperties.CREDENTIALS, new Credentials(requesterUserInfo.getUsername(), requesterUserInfo.getPassword())); } String uriStr = "ws://" + restEntryPointHost + ":" + restEntryPointPort + "/api/v1/event"; CountDownLatch messageLatch = new CountDownLatch(1); Session session = client.connectToServer(new Endpoint() { @Override public void onOpen(Session session, EndpointConfig config) { session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String messsage) { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(WebSocketMessage.class, new WebSocketMessageGsonAdapter()); Gson gson = builder.create(); WebSocketMessage response = gson.fromJson(messsage, WebSocketMessage.class); LOGGER.info("Receive WebSocket mesage : {}", response); if ("user".equals(response.getEntity()) && "authentication".equals(response.getAction()) && response.getData().has("message") && ((JsonObject) response.getData()) .getAsJsonPrimitive("message").getAsString().equals("success")) { receiveWebSocketWelcome = true; messageLatch.countDown(); } else { receiveWebSocketWelcome = false; } } }); if (requesterUserInfo != null) { try { String aggregateCredentials = String.format("%s:%s", requesterUserInfo.getUsername(), requesterUserInfo.getPassword()); String encodedCredentials = Base64.getEncoder() .encodeToString(aggregateCredentials.getBytes()); session.getBasicRemote() .sendText("{\n" + " \"entity\": \"user\",\n" + " \"action\": \"authentication\",\n" + " \"data\": {\n" + " \"authorization\": \"Basic " + encodedCredentials + "\"\n" + " }\n" + "}"); } catch (IOException e) { fail(e.getMessage()); } } } }, cec, new URI(uriStr)); messageLatch.await(10, TimeUnit.SECONDS); session.close(); } catch (Exception e) { if (expectSuccess) { fail(e.getMessage()); } else { receiveWebSocketWelcome = false; } } }
From source file:co.paralleluniverse.comsat.webactors.AbstractWebActorTest.java
private static Endpoint sendAndGetTextEndPoint(final String sendText, final SettableFuture<String> res) { return new Endpoint() { @Override//from www.j a v a 2 s . com public void onOpen(final Session session, EndpointConfig config) { session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String text) { res.set(text); } }); try { session.getBasicRemote().sendText(sendText); } catch (IOException ignored) { } } }; }
From source file:org.chtijbug.drools.platform.backend.wsclient.WebSocketClient.java
@Override public void onOpen(final Session session, EndpointConfig endpointConfig) { this.session = session; session.addMessageHandler(new MessageHandler.Whole<PlatformManagementKnowledgeBean>() { @Override// ww w .j ava 2 s.c o m public void onMessage(PlatformManagementKnowledgeBean bean) { switch (bean.getRequestRuntimePlarform()) { case jmxInfos: RealTimeParameters realTimeParameters = new RealTimeParameters(); PlatformRuntimeInstance targetplatformRuntimeInstance = platformRuntimeInstanceRepository .findByRuleBaseID(platformRuntimeInstance.getRuleBaseID()); realTimeParameters.setPlatformRuntimeInstance(targetplatformRuntimeInstance); JMXInfo jmxInfo = bean.getJmxInfo(); realTimeParameters.setAverageTimeExecution(jmxInfo.getAverageTimeExecution()); realTimeParameters.setMinTimeExecution(jmxInfo.getMinTimeExecution()); realTimeParameters.setMaxTimeExecution(jmxInfo.getMaxTimeExecution()); realTimeParameters.setTotalTimeExecution(jmxInfo.getTotalTimeExecution()); realTimeParameters.setTotalNumberRulesExecuted(jmxInfo.getTotalNumberRulesExecuted()); realTimeParameters.setAverageRulesExecuted(jmxInfo.getAverageRulesExecuted()); realTimeParameters.setMinRulesExecuted(jmxInfo.getMinRulesExecuted()); realTimeParameters.setMaxRulesExecuted(jmxInfo.getMaxRulesExecuted()); realTimeParameters.setNumberFireAllRulesExecuted(jmxInfo.getNumberFireAllRulesExecuted()); realTimeParameters.setAverageRuleThroughout(jmxInfo.getAverageRuleThroughout()); realTimeParameters.setMinRuleThroughout(jmxInfo.getMinRuleThroughout()); realTimeParameters.setMaxRuleThroughout(jmxInfo.getMaxRuleThroughout()); realTimeParametersRepository.save(realTimeParameters); jmxInfosListeners.messageReceived(platformRuntimeInstance.getRuleBaseID(), realTimeParameters); break; case versionInfos: versionInfosListener.messageReceived(platformRuntimeInstance.getRuleBaseID(), bean.getResourceFileList()); break; case isAlive: isAliveListener.messageReceived(platformRuntimeInstance.getRuleBaseID()); heartBeatListner.messageReceived(platformRuntimeInstance.getRuleBaseID(), bean.getHeartbeat().getLastAlive()); heartbeat.setLastAlive(bean.getHeartbeat().getLastAlive()); break; case loadNewRuleVersion: PlatformRuntimeDefinition platformRuntimeDefinitionloadNewRuleVersion = platformRuntimeDefinitionRepository .findByRuleBaseID(platformRuntimeInstance.getRuleBaseID()); platformRuntimeDefinitionloadNewRuleVersion .setCouldInstanceStartWithNewRuleVersion(bean.getRequestStatus().toString()); platformRuntimeDefinitionRepository.save(platformRuntimeDefinitionloadNewRuleVersion); loadNewRuleVersionListener.messageReceived(platformRuntimeInstance.getRuleBaseID(), bean.getRequestStatus(), bean.getResourceFileList()); break; } } }); }