SockJS Java Client Implementation for non-web application

前提是你 提交于 2019-12-08 09:35:06

问题


How do I implement SockJS client in java swing application with spring boot. if there have pretty good example please mention.


回答1:


First off, consult the official docs: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

A non-web application would still need an app server, in your case, one of the embedded solutions should definitely suffice. If you're using maven, try for example:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-client</artifactId>
        <version>9.4.0.v20161208</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-websocket</artifactId>
        <version>8.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-log4j</artifactId>
        <version>8.5.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Your client can look more or less like this:

List<Transport> transports = new ArrayList<Transport>(2);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());
    SockJsClient sockJsClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
    stompClient.setMessageConverter(new StringMessageConverter());
    StompSession session = null;
    DefaultStompFrameHandler stompHandler = new DefaultStompFrameHandler();
    try {
        session = stompClient.connect(WEBSOCKET_URI, new MyStompSessionHandler()).get(1, TimeUnit.SECONDS);
        session.subscribe("/topic" + "/channel", stompHandler);
        // do your stuff
        ...         
    } finally {
        if (session != null) {
            session.disconnect();
        }
    }

Your main Spring Boot class can initiate Swing Frame like so:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    ConfigurableApplicationContext context = new  SpringApplicationBuilder(Application.class).headless(false).run(args);

    EventQueue.invokeLater(() -> {
        // this is your JFrame
        AppPrincipalFrame appFrame = context.getBean(AppPrincipalFrame.class);
        appFrame.setVisible(true);
    });

Hopefully that will be of some help :) Good luck!



来源:https://stackoverflow.com/questions/41870207/sockjs-java-client-implementation-for-non-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!