Socket长连接建立连接报错 WebSocket connection to ‘ws://localhost:8080/bms/chat/1‘ failed:

问题

Socket长连接建立连接报错 WebSocket connection to ‘ws://localhost:8080/bms/chat/1’ failed:

详细问题

笔者使用技术框架Servlet + JSP,基于Socket长连接实现实时聊天室
JSP核心代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <title>实时聊天室</title>
    <script>
    	<c:set var="chatObjectId" value="${chatObjectId}"/>
        var socket;
        function connect() {
    
    
            socket = new WebSocket("ws://localhost:8080/bms/chat?chatObjectId=" + ${
    
    ChatObjectId});
            socket.onopen = function () {
    
    
                console.log("WebSocket连接已打开");
            };

            socket.onmessage = function (event) {
    
    
                var message = event.data;
                // 处理接收到的消息,例如将其显示在聊天窗口中
                var chatWindow = document.getElementById("chat-window");
                var newMessage = document.createElement("p");
                newMessage.textContent = message;
                chatWindow.appendChild(newMessage);
            };
        }

        function sendMessage() {
    
    
            var input = document.getElementById("message");
            var message = input.value;
            socket.send(message);
            input.value = "";
        }

        function disconnect() {
    
    
            socket.close();
        }
    </script>
</head>
<body>
    <div id="chat-window"></div>
    <input type="text" id="message" />
    <button onclick="sendMessage()">发送</button>
    <button onclick="connect()">连接</button>
    <button onclick="disconnect()">断开连接</button>
</body>
</html>

java核心代码

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@ServerEndpoint("/chat")
public class ChatServer {
    
    

    private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<>());
    private static Map<Integer, Session> userSessions = Collections.synchronizedMap(new HashMap<>());


    @OnOpen
    public void onOpen(Session session, @PathParam("ChatObjectId") String ChatObjectId) {
    
    
        sessions.add(session);
        // 将字符串转换为整数类型的用户ID
        Integer ChatObjectId= Integer.parseInt(ChatObjectId); 
         // 将会话与userId关联,并添加到userSessions集合中
        userSessions.put(ChatObjectId, session);
    }

    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
    
    
        try {
    
    
            Jsonb jsonb = JsonbBuilder.create();
            MessagePayload payload = jsonb.fromJson(message, MessagePayload.class);
            Integer targetUserId = payload.getUserId();
            String content = payload.getContent();
            sendToUser(targetUserId, content);
        } catch (JsonbException e) {
    
    
            e.printStackTrace();
        }
    }

    public void sendToUser(Integer userId, String message) throws IOException {
    
    
        Session session = userSessions.get(userId);
        if (session != null) {
    
    
            session.getBasicRemote().sendText(message);
        }
    }

    @OnClose
    public void onClose(Session session) {
    
    
        sessions.remove(session);
    }

    @OnError
    public void onError(Throwable error) {
    
    
        error.printStackTrace();
    }
}

运行项目,游览器控制台报错

WebSocket connection to 'ws://localhost:8080/bms/chat/1' failed

解决方案

对于java代码ChatServer类的注解由

@ServerEndpoint("/chat")

改为

@ServerEndpoint("/chat/{chatObjectId}")

解决原因

@ServerEndpoint注解的值应该与你在JSP中WebSocket连接的URL路径一致。笔者JSP代码中连接的URL路径是ws://localhost:8080/bms/chat?chatObjectId=1,但是笔者Java代码中的@ServerEndpoint注解值为/chat。
因此需要将@ServerEndpoint注解的值改为/chat/{chatObjectId}。这样,URL路径中的chatObjectId将作为路径参数传递给onOpen方法。
笔者误以为只要在onOpen传递参数就可以

参考文献

解决方案参考chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/T_Y_F_/article/details/131025788
今日推荐