非 Web 应用程序的 SockJS Java 客户端实现 [英] SockJS Java Client Implementation for non-web application

查看:43
本文介绍了非 Web 应用程序的 SockJS Java 客户端实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Spring Boot 在 java swing 应用程序中实现 SockJS 客户端.如果有很好的例子请提及.

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

推荐答案

首先参考官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

非 Web 应用程序仍需要应用程序服务器,就您而言,其中一种嵌入式解决方案绝对足够了.如果您使用的是 Maven,请尝试例如:

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();
        }
    }

您的主要 Spring Boot 类可以像这样启动 Swing Frame:

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!

这篇关于非 Web 应用程序的 SockJS Java 客户端实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆