簡體   English   中英

非Web應用程序的SockJS Java客戶端實現

[英]SockJS Java Client Implementation for non-web application

如何使用Spring Boot在Java Swing應用程序中實現SockJS客戶端。 如果有很好的例子,請提及。

首先,請查閱官方文檔: https : //docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

非Web應用程序仍將需要一台應用程序服務器,在您的情況下,其中一種嵌入式解決方案就足夠了。 如果您使用的是maven,請嘗試以下示例:

<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>

您的客戶或多或少看起來像這樣:

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:

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

希望這會有所幫助:)祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM