jQuery无法连接到Spring WebSocket

时间:2019-12-20 10:13:00

标签: javascript spring spring-websocket

我正在尝试使用Spring和JS实现推送通知。

我的服务配置如下:

@Controller
public class GreetingController {


  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(String namethrows Exception {
    return new Greeting("Hello, " + name "!");
  }

}

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

  @Override
  public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
  }

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/gs-guide-websocket").withSockJS();
  }

}

在客户端上,我尝试连接到服务:

var ws = new WebSocket('ws://localhost:8810/gs-guide-websocket');

但是在控制台中,我看到消息:

Firefox can't establish a connection to the server at ws://localhost:8810/gs-guide-websocket

如果我尝试访问该URL,请打个招呼:Welcome to SockJS!

为什么JS无法连接到套接字?

0 个答案:

没有答案