【问题标题】:Failed to load resource with Websocket application无法使用 Websocket 应用程序加载资源
【发布时间】:2014-08-22 05:59:40
【问题描述】:

我正在使用 Spring Boot 和 Tomcat 7 使用 STOMP 和 sockJS 创建一个带有 Websockets 的 Web 应用程序。以下是我的请求映射的类:

@Controller
public class HelloController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        return new Greeting("Hello, " + message.getName() + "!");
    }
}

很遗憾,我在网页上的控制台中收到以下错误:

Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found)             http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined
Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found)             http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined

你好是我的大部分代码包,但我不确定导致错误的“信息”目录是什么。有没有人可以帮助我?

编辑: 这是我的 Javascript 客户端代码:

function connect() {
        var socket = new SockJS('/WarTest/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

    function disconnect() {
        stompClient.disconnect();
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName() {
        var name = document.getElementById('name').value;
        stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
    }

    function showGreeting(message) {
        var response = document.getElementById('response');
        var p = document.createElement('p');
        p.style.wordWrap = 'break-word';
        p.appendChild(document.createTextNode(message));
        response.appendChild(p);

还有我的 websocketconfig:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

编辑:

WebInitializer.java

public class WebInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)    {
        return application.sources(Application.class);
    }

}

EDIT2:

WebsocketConfig.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}

EDIT3:

当我使用 SockJS('http://localhost:8080/WarTest/hello'): 直接从 IDE 启动它时,我得到了这个

Opening Web Socket... stomp.js:130
Web Socket Opened... stomp.js:130
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

stomp.js:130
<<< CONNECTED
heart-beat:0,0
version:1.1

stomp.js:130
connected to server undefined stomp.js:130
Connected: CONNECTED
version:1.1
heart-beat:0,0

(index):23
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings

但是,当我作为 WAR 部署到 Tomcat 时,我收到了找不到 URL/信息的错误。

【问题讨论】:

  • 您应该显示用于连接的端点配置和客户端代码。看起来您使用错误的 URL 连接到 WebSocket 端点
  • 感谢 Artem 的及时回复!我添加了一些我的实现。让我知道这是否有帮助,或者我还有什么可以补充的!
  • 请显示,您如何注册DispatcherServlet?或者您只是想这样做,但再显示一次 WebSocketConfig...
  • 抱歉,搞混了。更新了 WebsocketConfig 和 WebInitializer
  • 您能否显示用于访问带有 WebSocket 客户端代码的页面的 URL?那是来自同一个 WAR 的页面吗?

标签: tomcat spring-boot spring-websocket


【解决方案1】:

看起来您应该将应用程序上下文添加到 SockJS 构造函数。来自 Spring 参考手册:

var socket = new SockJS("/spring-websocket-portfolio/portfolio");

配置在哪里:

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

    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/portfolio").withSockJS();
    }

但是 WebApp 是在上下文 /spring-websocket-portfolio 下部署的。

更新

如果您显示您的 web 应用程序的 URL,那就太好了。假设您应该使用来自 SockJS 构造函数的 WebSocket 端点的 完整 URL:

var socket = new SockJS("http://localhost:8080/WarTest/hello");

更新2

你应该做几件事:

  1. Web 应用程序部署在某些上下文中。我想,在你的情况下是WarTest。通常是不带.war 扩展名的WAR 名称。

  2. Spring MVC 应用程序基于DispatcherServlet,它在 webapp 配置(web.xmlWebApplicationInitializer)中注册,并带有一些映射。例如。 /spring

  3. 每个服务(Controller、它的方法或 WebSocket 端点)都在某个 URI 路径下注册。在您的情况下,它是/hello

所以,你完整的 SockJS URL 应该是这样的:

http://localhost:8080/WarTest/spring/hello

如果您正确指定了port

【讨论】:

  • 谢谢!抱歉,在上下文“/spring-websocket-portfolio”下部署是什么意思?我需要更改包裹的名称吗? (目前是“你好”)。我还需要将“hello”的其他引用更改为“portfolio”吗?
  • 当然不是:这只是一个样本。您如何通过 HTTP 访问您的应用程序 - http://localhost:8080/...?或者你如何从浏览器调用index页面?
  • 啊,是的!我现在知道了。太感谢了。它现在工作得非常好!非常感谢您的帮助。
  • 嗨 Artem,当我从 Eclipse 运行我的应用程序时,它现在可以工作了,但是当我尝试将其作为战争部署到 Tomcat 时,我得到了与以前相同的错误。有什么想法吗?
  • 我已经编辑了我的问题以反映我的新问题,尽管它基本上与我现在作为 WAR 部署到 Tomcat 而不是使用 Eclipse 启动独立实例的性质相同。
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 2019-05-10
  • 2022-10-30
相关资源
最近更新 更多