There is an example of connecting to the socket on javascript.

self.ws = new WebSocket((window.location.protocol == "http:" ? "ws://" : "wss://") + window.location.host + "/api/get"); self.ws.onopen = function () { if (self.__ws_task) clearInterval(self.__ws_task); self.ws.send( JSON.stringify({ command: "subscribe", data: 'test1:test2', data2: '{temp:[32, 31, 41, 51, 55, 54, 61, 91], temp2:[1018433255,805863782,-3330707801]}' }) ); }; 

How to do the same in Java? And then still receive CONSTANTLY messages in response?

I tried this:

  final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("ws://temp.com/api/get")); clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() { public void handleMessage(String message) { System.out.println("message"); System.out.println(message); } }); 

WebsocketClientEndpoint.java

 import org.codehaus.jettison.json.JSONObject; import javax.websocket.*; import java.net.URI; @ClientEndpoint public class WebsocketClientEndpoint { Session userSession = null; private MessageHandler messageHandler; public WebsocketClientEndpoint(URI endpointURI) { try { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); container.connectToServer(this, endpointURI); } catch (Exception e) { throw new RuntimeException(e); } } /** * Callback hook for Connection open events. * * @param userSession the userSession which is opened. */ @OnOpen public void onOpen(Session userSession) { System.out.println("opening websocket"); this.userSession = userSession; JSONObject obj = new JSONObject(); try { obj.put("command", "subscribe"); obj.put("data", "test1:test2"); obj.put("data2", "{temp:[32, 31, 41, 51, 55, 54, 61, 91], temp2:[1018433255,805863782,-3330707801]}"); String message = obj.toString(); } catch (Exception e) { e.printStackTrace(); } String message = obj.toString(); System.out.println(message); this.sendMessage(message); } /** * Callback hook for Connection close events. * * @param userSession the userSession which is getting closed. * @param reason the reason for connection close */ @OnClose public void onClose(Session userSession, CloseReason reason) { System.out.println("closing websocket"); this.userSession = null; } /** * Callback hook for Message Events. This method will be invoked when a client send a message. * * @param message The text message */ @OnMessage public void onMessage(String message) { if (this.messageHandler != null) { this.messageHandler.handleMessage(message); } } /** * register message handler * * @param msgHandler */ public void addMessageHandler(MessageHandler msgHandler) { this.messageHandler = msgHandler; } /** * Send a message. * * @param message */ public void sendMessage(String message) { System.out.println("send message"); this.userSession.getAsyncRemote().sendText(message); } /** * Message handler. * * @author Jiji_Sasidharan */ public static interface MessageHandler { public void handleMessage(String message); } } 

I get this result:

 opening websocket {"command":"subscribe","data":"test1:test2","data2":"{temp:[32, 31, 41, 51, 55, 54, 61, 91], temp2:[1018433255,805863782,-3330707801]}"} send message 

Thank you all in advance for any advice!

  • Need to receive messages on the side of JS or Java? - Vyacheslav Gusser
  • It is necessary to receive messages on the side of JAVA - jessez
  • I'll try to help you after work - Vyacheslav Gusser

0