WebSocket连接已建立,但客户端无法接收来自其他人的消息

时间:2015-12-09 08:47:38

标签: java spring websocket spring-websocket

我使用的是Java Spring 4,我所遵循的教程是 Using websockets in Java using Spring 4

我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    ...
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.2.0.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>

    <dependencies>
        ...

        <!-- Web Socket -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-websocket</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
    </dependencies>
</project>

我的WebSocketHandler:

package com.example.smvcws;

import org.apache.log4j.Logger;
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 {
        TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");
        session.sendMessage(returnMessage);
    }
}

这是我的XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    xsi:schemaLocation="http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    ... 

    <beans:bean id="websocketEndPoint" class="com.example.smvcws.WebsocketEndPoint" />
    <websocket:handlers allowed-origins="*">
        <websocket:mapping path="/websocket" handler="websocketEndPoint" />
        <websocket:handshake-interceptors>
            <beans:bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor" />
        </websocket:handshake-interceptors>
    </websocket:handlers>
</beans:beans>

我的网页的HTML(jsp)代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Websocket Test</title>
</head>
<body>
    <textarea></textarea>
    <button>send</button>
    <div id="result"></div>

    <script type="text/javascript">
        var select = document.querySelector.bind(document)
        var ws = new WebSocket('ws://' + location.host + "<%=request.getContextPath()%>/websocket")
        ws.addEventListener("message", function(e){
            var div = document.createElement("div")
            div.innerHTML += "<span>" + e.data + "</span>"
            div.innerHTML += " @ "
            div.innerHTML += "<time>" + (new Date(e.timeStamp)).toLocaleString() + "</time>" 
            select("#result").appendChild(div)
        })

        select("button").addEventListener("click", function (e) {
            switch(ws.readyState){
                case WebSocket.OPEN:
                case WebSocket.CONNECTING:
                    ws.send(select("textarea").value)
                    break
                case WebSocket.CLOSED:
                case WebSocket.CLOSING:
                    select("#result").innerHTML += "<div style='color:FireBrick'>Error: Websocket is closed (<time>" + (new Date).toLocaleString() + "</time>)</div>"
                    break
            }

        })
    </script>
</body>
</html>

websocket连接成功。如果我发送任何消息,<div id="result">将打印该消息。

但是,我无法接收别人的消息。

例如:我使用2个浏览器A,B链接页面。我在浏览器A中发送消息。消息将显示在浏览器A 中,但不会显示在浏览器B中

1 个答案:

答案 0 :(得分:2)

您只是将消息发送到启动通信开始的一个会话...请查看API,因为我不熟悉Spring 4 ...应该是一种向所有连接的客户端发送消息的方式,而不仅仅是会话。