【问题标题】:Starting a simple WebSocket Server with Spring integration使用 Spring 集成启动一个简单的 WebSocket 服务器
【发布时间】:2016-02-14 16:24:05
【问题描述】:

我正在尝试在 spring 集成应用程序中启动一个简单的 websocket 服务器。

由于以下依赖项,我构建了我的应用程序:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-integration")
    compile("org.springframework:spring-websocket")
    compile("org.springframework.integration:spring-integration-ip")
    compile("org.springframework.integration:spring-integration-websocket")
    compile('com.fasterxml.jackson.core:jackson-databind')
    compile("org.springframework.integration:spring-integration-feed")   
}

感谢以下 Java 文件,我启动了我的应用程序:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
org.springframework.integration.annotation.IntegrationComponentScan;

@Configuration
@EnableAutoConfiguration
@IntegrationComponentScan
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new SpringApplication("/integration.xml").run(args);
        System.out.println("Hit Enter to terminate");
        System.in.read();
        ctx.close();
    }

}

这是我的“integartion.xml”:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:file="http://www.springframework.org/schema/integration/file"
    xmlns:feed="http://www.springframework.org/schema/integration/feed"
        xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
        xmlns:ws="http://www.springframework.org/schema/integration/websocket"
    xsi:schemaLocation="http://www.springframework.org/schema/integration/feed http://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
                http://www.springframework.org/schema/integration/websocket http://www.springframework.org/schema/integration/websocket/spring-integration-websocket.xsd">

    <int-ip:udp-inbound-channel-adapter id="udpReceiver"
        channel="udpOutChannel"
        port="11111"
        receive-buffer-size="500"
        multicast="true"
        multicast-address="225.6.7.8"/>
    <int:object-to-string-transformer input-channel="udpOutChannel" output-channel="stringified"/>
    <bean id="nmeaParser" class="transformers.NMEAParser"/>   
    <int:transformer input-channel="stringified" ref="nmeaParser" output-channel="parsedData"/>   
    <int:channel id="jsondata"/>
    <int:object-to-json-transformer input-channel="parsedData" output-channel="jsondata"/>
    <ws:server-container id="server" path="/position"/>        
    <ws:outbound-channel-adapter container="server" channel="jsondata"/>
</beans>

如您所见,我创建了一个 UDP 入站通道。这个频道就像一个魅力。我可以看到数据已成功传递给我的“NMEAParser”bean。

我认为我的 Websocket 服务器无法正常工作,因为:

我无法使用 wscat 连接到它...当我尝试这样做时:

wscat -c ws://127.0.0.1:8080/position

我收到“CONNECTION REFUSED”,与未启动任何内容时收到的消息相同。

在 Spring 日志中我可以读到这个

Starting beans in phase 0    
Adding {object-to-string-transformer} as a subscriber to the 'udpOutChannel' channel    
Channel 'application:8080.udpOutChannel' has 1 subscriber(s).   
started org.springframework.integration.config.ConsumerEndpointFactoryBean#0    
Adding {transformer} as a subscriber to the 'stringified' channel    
Channel 'application:8080.stringified' has 1 subscriber(s).    
started org.springframework.integration.config.ConsumerEndpointFactoryBean#1    
Adding {object-to-json-transformer} as a subscriber to the 'parsedData' channel    
Channel 'application:8080.parsedData' has 1 subscriber(s).    
started org.springframework.integration.config.ConsumerEndpointFactoryBean#2    
Adding {websocket:outbound-channel-adapter} as a subscriber to the 'jsondata' channel    
Channel 'application:8080.jsondata' has 1 subscriber(s).    
started org.springframework.integration.config.ConsumerEndpointFactoryBean#3    
Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel    
Channel 'application:8080.errorChannel' has 1 subscriber(s).    
started _org.springframework.integration.errorLogger    
Starting beans in phase 1073741823    
started udpReceiver    
Started Application in 0.792 seconds (JVM running for 1.121)

我们可以在这里看到我的所有 bean 都已启动,除了作为我的服务器容器的 bean“服务器”。此日志消息中没有此 bean 的踪迹。

为了编写我的应用程序,我仔细阅读了 this sampethis doc

我做错了什么,或者我忘记了什么?

【问题讨论】:

  • 您如何部署您的应用程序?你用的是哪个websocket runtime
  • 有趣的问题。实际上我没有选择任何 websocket 运行时。我用我的 Main.java 更新了我的问题...
  • 在示例中,有这一行:@ImportResource(。这一行是否允许 Spring 集成实例化 websocket 运行时?
  • 很好 - 这是一个 Spring Boot 应用程序,因此您将获得一个运行时。默认情况下它将在端口 8080 上运行,而不是 80。试试wscat -c ws://127.0.0.1:8080/position
  • 哎呀,对不起,我的问题不是最新的......所以我更新了它。实际上我尝试了 8080 端口,但我的连接被拒绝了。

标签: spring spring-integration


【解决方案1】:

正如我们在冗长的评论后的聊天中所讨论的,您的依赖项中需要org.springframework.boot:spring-boot-starter-websocket,这将引入spring-boot-starter-weborg.springframework.boot:spring-boot-starter-tomcat,从而为您提供一个websocket 运行时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多