Java Code Examples for org.springframework.web.socket.client.standard.StandardWebSocketClient

Following code examples demonstrate how to use org.springframework.web.socket.client.standard.StandardWebSocketClientfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.socket.client.standard.StandardWebSocketClientand various code implementation of this class.

    public static void main(String[] args) {
        List<Transport> transports=new ArrayList<Transport>(2);
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());

        SockJsClient sockJsClient=new SockJsClient(transports);
    } 


    public static void main(String[] args) throws Exception {
            WebSocketClient client = new StandardWebSocketClient();
            ListenableFuture<WebSocketSession> future = client.doHandshake(new AbstractWebSocketHandler() {
                @Override
                public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
                    System.out.println(message.getPayload());
                }
            }, url);

            WebSocketSession socketSession = future.get();
            socketSession.sendMessage(new TextMessage("{\"command\":\"test\",\"parameters\":{\"type\":\"conn\"}}"));
        System.in.read();
    } 

    public void initiate(){
        List<Transport> transports = new ArrayList<>(2);
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());

        SockJsClient sockJsClient = new SockJsClient(transports);


    } 

    public static void main(String[] args) {
        List<Transport> transports = new ArrayList<Transport>(2);
        transports.add(new WebSocketTransport(new StandardWebSocketClient()));
        transports.add(new RestTemplateXhrTransport());

        SockJsClient sockJsClient = new SockJsClient(transports);
    } 

	public static void main(String[] args) {
		SpringApplication.run(LearnWebsocketApplication.class, args);


		StandardWebSocketClient standardWebSocketClient = new StandardWebSocketClient();
		
	} 

    public WebSocketStompClient createWebSocketClient() {
        WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());
        stompClient.setMessageConverter(new StringMessageConverter());
        return stompClient;
    } 

    public WebSocketConnectionManager connect(TextWebSocketHandler handler, String url, HttpHeaders headers) {

        WebSocketConnectionManager manager = new WebSocketConnectionManager(new StandardWebSocketClient(), handler, url);
        manager.setHeaders(headers);
        manager.setAutoStartup(true);

        return manager;
    } 

    public static void main(String[] args) {
        WebSocketClient client = new StandardWebSocketClient();
        WebSocketStompClient stompClient = new WebSocketStompClient(client);

        stompClient.setMessageConverter(new MappingJackson2MessageConverter());

        StompSessionHandler sessionHandler = new MyStompSessionHandler();
        stompClient.connect(URL, sessionHandler);

    } 

	public static void main(String[] args) throws Throwable {

		ScriptEngineManager sem = new ScriptEngineManager();
		ScriptEngine engine = sem.getEngineByName("javascript");

		List<Transport> transports = new ArrayList<>(1);
		transports.add(new WebSocketTransport( new StandardWebSocketClient()) );
		WebSocketClient transport = new SockJsClient(transports);


		WebSocketStompClient stompClient = new WebSocketStompClient(transport);
		stompClient.setMessageConverter(new StringMessageConverter());
		StompSessionHandler handler = new Handler() ;
		ListenableFuture<StompSession> session_f = stompClient.connect(WS_URI, handler);
		StompSession session = session_f.get();
		session.send("/w/api/node/info","");
		session.subscribe("/blocks/new", new StompFrameHandler() {
				@Override
				public Type getPayloadType(StompHeaders headers) {
				return String.class;
				}

				@Override
				public void handleFrame(StompHeaders headers, Object payload) {
					System.out.println("NEW BLOCK \n\n");
					System.out.println(payload);
					String script = "Java.asJSONCompatible(" + payload + ")";
					try {
						Object result = engine.eval(script);
						Map contents = (Map) result;
						contents.forEach((t, u) -> {
								System.out.println( t + " -> " + u);
								});
					}
					catch (Exception e){
					}

				}

				});
		Thread.sleep(1000*60*10);
		System.out.println("end");
	} 

    public WebSocketClient webSocketClient() {
        WebSocketClient client = new StandardWebSocketClient();
        return client;
    } 

Advertisement
Javadoc
A WebSocketClient based on standard Java WebSocket API. @author Rossen Stoyanchev @since 4.0
Advertisement