浏览器无法与spring4 websocket服务器连接

时间:2014-06-26 03:30:37

标签: java spring web websocket

在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无法从firefox Web控制台连接到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);
    }
}

HandshakeInterceptor.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中的项目名称。

感谢您的回答。

2 个答案:

答案 0 :(得分:0)

我没有使用Spring websocket。但我发现你的网址似乎错了。书来自哪里? Firefox希望与&#34; ws://localhost/Books/websocket.ws"连接。但在您的配置中,ws端点绑定到&#34; /websocket.ws"。

答案 1 :(得分:0)

最近我遇到了同样的问题,我整天试图解决它。最后我意识到我需要通过DispatcherServlet而不是ContextLoaderListener读取websocket:handlers标签。

   <servlet>
   <servlet-name>Spring4Sagussoft</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
   <servlet-name>Spring4Sagussoft</servlet-name>
   <url-pattern>/</url-pattern>
   </servlet-mapping>

并进入webmvc-config.xml

   <websocket:handlers>
    <websocket:mapping path="/myHandler" handler="myHandler" />
   </websocket:handlers>
   <bean id="myHandler" class="com.laresistencia.pe.spring4.websocket.MyHandler" />

这适合我。