2015-02-03 14 views
8

Sto lavorando a un client Stomp Spring WebSocket per il mio server WebSocket e ricevo informazioni contrastanti. Ho trovato 2 modi per farlo funzionare e senza entrare troppo nel dettaglio Mi chiedevo in che modo è considerato il modo "corretto" di implementare il client.Come implementare correttamente un client java spring-websocket

Qualcuno potrebbe aiutarmi a capire a cosa serve WebSocketConnectionManager?

Inoltre, un'altra domanda, come mantenere la connessione web socket aperta e il programma in esecuzione per accettare nuovi messaggi senza dover scrivere la riga System.in.read().

1'st modo: Usando il SockJsClient direttamente

URI uri = new URI("ws://localhost:8080/stomp"); 
StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient(); 

List<Transport> transports = new ArrayList<>(1); 
transports.add(new WebSocketTransport(simpleWebSocketClient)); 

SockJsClient sockJsClient = new SockJsClient(transports); 
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec()); 

StompMessageReceiver messageHandler = new StompMessageReceiver(); 
StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter()); 

try { 
    this.webSocketClient.doHandshake(websocketHandler, null, uri).get(); 
} catch (InterruptedException | ExecutionException e) { 
    throw new IllegalStateException(e); 
} 

System.in.read(); 

2'nd modo: Usando il WebSocketConnectionManager

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

SockJsClient sockJsClient = new SockJsClient(transports); 
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec()); 

StompMessageHandler messageHandler = new StompMessageHandler(); 
StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter()); 

WebSocketConnectionManager manager = new WebSocketConnectionManager(sockJsClient, websocketHandler, "ws://localhost:8080/stomp"); 

manager.start(); 

System.in.read(); 

So che avrei potuto fare questo molto più semplice utilizzando le annotazioni per @Configuration e @Bean ma sto provando a fare un'implementazione "grezza" così posso capire come tutto funziona insieme.

Un po 'più di informazioni:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-websocket</artifactId> 
    <version>4.1.4.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-messaging</artifactId> 
    <version>4.1.4.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>javax.websocket</groupId> 
    <artifactId>javax.websocket-client-api</artifactId> 
    <version>1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.tomcat</groupId> 
    <artifactId>tomcat-websocket</artifactId> 
    <version>8.0.0-RC10</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.5.0</version> 
</dependency> 

risposta

3

Se è interessante integrazione di Spring fornisce un'implementazione per il WebSocketClient.

E sì, internamente utilizza ConnectionManagerSupport.

Questo è un test-case che mostra come configurarlo da @Configuration.

Ma penso che dovresti provare con l'implementazione WebSocketHandler pronta per l'uso - SubProtocolWebSocketHandler e StompSubProtocolHandler.

Problemi correlati