浏览器可以与Spring 4 WebSocket服务器连接

时间:2014-06-23 08:20:38

标签: java javascript spring websocket

的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>

的applicationContext.xml

<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>

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);
    }
}

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);
    }
}

但我无法连接它。

javascript代码:

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

这个网址错了吗? “书籍”是我在tomcat中的项目名称。 你知道什么是错的吗?谢谢你的回答。

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

0 个答案:

没有答案