WebSocket Connection Established, but Client Can't Receive Messages from Others

萝らか妹 提交于 2019-12-12 06:05:48

问题


I use Java Spring 4 and the tutorial I followed is Using websockets in Java using Spring 4

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

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

This is my XML configuration:

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

My page's HTML (jsp) code:

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

The websocket connection is successful. If I send any message, <div id="result"> will print the message.

However, I cannot receive others’ message.

For example: I use 2 browser A, B to link the page. The I send a message in browser A. The message will displayed in browser A but not in browser B.


回答1:


You're only sending the message to the one session that initiated the communication to begin with... Have a look at the API for it, as I'm not familiar with Spring 4... There should be a way to send a message to all connected clients, and not just the session.



来源:https://stackoverflow.com/questions/34174333/websocket-connection-established-but-client-cant-receive-messages-from-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!