gpt4 book ai didi

java - 浏览器无法连接 spring4 websocket 服务器

转载 作者:行者123 更新时间:2023-11-29 08:47:24 25 4
gpt4 key购买 nike

在 Tomcat 7.0.50 下运行的工作 Spring MVC 4.0.5 项目中,遵循 the tutorial .

当我启动 Tomcat 时,在 eclipse 控制台(catalina 日志)中,出现行

Mapped URL path [/websocket.ws] onto handler of type [class org.springframework.web.socket.server.support.WebSocketHttpRequestHandler]

我从 Firefox Web 控制台收到错误“Firefox 无法连接到 ws://localhost/Books/websocket.ws”[HTTP/1.1 404 Not Found 6ms]。

我的配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

applicationContext.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:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">


<bean id="websocket" class="com.example.websocket.handler.WebsocketEndPoint" />

<websocket:handlers>
<websocket:mapping path="/websocket.ws" handler="websocket" />
<websocket:handshake-interceptors>
<bean class="com.example.websocket.HandshakeInterceptor" />
</websocket:handshake-interceptors>
</websocket:handlers>

</beans>

WebsocketEndPoint.java

package com.example.websocket.handler;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

public class WebsocketEndPoint extends TextWebSocketHandler{

@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception{
super.handleTextMessage(session, message);
System.out.println(message.getPayload());
TextMessage returnMessage =new TextMessage(message.getPayload()+"received at server");
session.sendMessage(returnMessage);
}
}

握手拦截器.java

package com.example.websocket;

import java.util.Map;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{

@Override
public boolean beforeHandshake(ServerHttpRequest request,
ServerHttpResponse response,WebSocketHandler wsHandler,
Map<String,Object> attributes) throws Exception{
System.out.println("Before Handshake!");
return super.beforeHandshake(request,response,wsHandler,attributes);
}

@Override
public void afterHandshake(ServerHttpRequest request,
ServerHttpResponse response,WebSocketHandler wsHandler,
Exception ex){
System.out.println("After HandShake!");
super.afterHandshake(request,response,wsHandler,ex);
}
}

javascript代码:

Chat.socket = new WebSocket('ws://127.0.0.1:80/Books/websocket.ws');

web.xml 是不是错了?

"Books"是我在 tomcat 中的项目名称。

感谢您的回答。

最佳答案

我没有使用 Spring websocket。但是我发现你的url好像不对。书从哪里来? Firefox 想要连接到“ws://localhost/Books/websocket.ws”。但是在您的配置中,ws 端点绑定(bind)到“/websocket.ws”。

关于java - 浏览器无法连接 spring4 websocket 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com