在Spring中运行javax.websocket端点?

huangapple go评论173阅读模式
英文:

running a javax.websocket endpoint in spring?

问题

我已经按照这个教程实现了一个WebSocket服务器:https://www.baeldung.com/java-websockets

现在我该如何运行这个应用程序?我正在使用Spring,我的主要函数如下:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class App {
  5. public static void main(String[] args) {
  6. SpringApplication.run(App.class, args);
  7. }
  8. }

我的WebSocket端点看起来像这样:

  1. import java.io.IOException;
  2. import javax.websocket.*;
  3. import javax.websocket.server.*;
  4. @ServerEndpoint(
  5. value = "/chat/{username}",
  6. decoders = MessageDecoder.class,
  7. encoders = MessageEncoder.class
  8. )
  9. public class Controller {
  10. // ...
  11. }

现在,当我运行这个应用程序,并尝试像这样连接到WebSocket端点:wsta ws://localhost:8080/chat/aa -I,我会收到以下错误:

> WebSocket升级请求
>
> ---
>
> 主机:localhost:8080
>
> 连接:升级
>
> 升级:websocket
>
> Sec-WebSocket-Version:13
>
> Sec-WebSocket-Key:...
>
> 来源:http://localhost
>
>
> WebSocket升级响应
>
> ---
>
> 404 未找到
>
> Vary:OriginAccess-Control-Request-MethodAccess-Control-Request-Headers
>
> Content-Type:application/json
>
> Transfer-Encoding:分块传输
>
> 日期:2020年5月5日星期二,12:12:52 GMT
>
>
> WebSocketError:WebSocket响应错误

这里的答案(https://stackoverflow.com/a/57924245/10551293)指出,您可以通过ws://localhost:8080/context/chat/aa访问WebSocket端点。这里的"context"是什么意思,我如何使服务器运行并接受传入连接?

英文:

I have implemented a websocket server by following this tutorial: https://www.baeldung.com/java-websockets

Now how do I run this app? I am using Spring and my main function looks like this:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. @SpringBootApplication
  4. public class App {
  5. public static void main(String[] args) {
  6. SpringApplication.run(App.class, args);
  7. }
  8. }

and my websocket endpoint looks like this:

  1. import java.io.IOException;
  2. import javax.websocket.*;
  3. import javax.websocket.server.*;
  4. @ServerEndpoint(
  5. value = "/chat/{username}",
  6. decoders=MessageDecoder.class,
  7. encoders = MessageEncoder.class
  8. )
  9. public class Controller {
  10. // ...
  11. }

now when I run this, and try to connect to the websocket endpoint like this: wsta ws://localhost:8080/chat/aa -I I get the following error:

> WebSocket upgrade request
>
> ---
>
> Host: localhost:8080
>
> Connection: Upgrade
>
> Upgrade: websocket
>
> Sec-WebSocket-Version: 13
>
> Sec-WebSocket-Key: ...
>
> Origin: http://localhost
>
>
> WebSocket upgrade response
>
> ---
>
> 404 Not Found
>
> Vary: OriginAccess-Control-Request-MethodAccess-Control-Request-Headers
>
> Content-Type: application/json
>
> Transfer-Encoding: chunked
>
> Date: Tue, 05 May 2020 12:12:52 GMT
>
>
> WebSocketError: WebSocket response error

The answer here (https://stackoverflow.com/a/57924245/10551293) says that you access the webscoket endpoint over ws://localhost:8080/context/chat/aa. What is context, and how do I get my server to run and accept incoming connections?

答案1

得分: 2

为了使OP提到的Baeldung教程正常运行,我不得不在pom.xml中添加一个spring-boot-starter-websocket的Maven依赖项:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-websocket</artifactId>
  4. </dependency>

然后,我添加了一个Spring @Configuration,其中包含了@EnableWebSocket注解,以及一个实例化的ServerEndpointExporter bean:

  1. @Configuration
  2. @EnableWebSocket
  3. public class WsConfig {
  4. @Bean
  5. public ServerEndpointExporter serverEndpoint() {
  6. return new ServerEndpointExporter();
  7. }
  8. }

此外,需要将端点控制器标记为Spring @Component

  1. @ServerEndpoint(
  2. value = "/chat/{username}",
  3. decoders = MessageDecoder.class,
  4. encoders = MessageEncoder.class
  5. )
  6. @Component
  7. public class Controller {
  8. // ...
  9. }

最终,WebSocket端点可通过ws://localhost:8080/chat/torvalds访问。

英文:

In order to get the Baeldung tutorial mentioned in OP running

  1. I had to add a maven dependency for spring-boot-starter-websocket to pom.xml

    1. &lt;dependency&gt;
    2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    3. &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
    4. &lt;/dependency&gt;
  2. Add a spring @Configuration in which @EnableWebSocket is added and a ServerEndpointExporter bean is instantiated:

    1. @Configuration
    2. @EnableWebSocket
    3. public class WsConfig {
    4. @Bean
    5. public ServerEndpointExporter serverEndpoint() {
    6. return new ServerEndpointExporter();
    7. }
    8. }
  3. Additionally the endpoint controller has to be a spring @Component:

    1. @ServerEndpoint(
    2. value = &quot;/chat/{username}&quot;,
    3. decoders=MessageDecoder.class,
    4. encoders = MessageEncoder.class
    5. )
    6. @Component
    7. public class Controller {
    8. ...

<hr>
Eventually the websocket endpoint was reachable under ws://localhost:8080/chat/torvalds

huangapple
  • 本文由 发表于 2020年5月5日 20:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定