WebSocket连接是404

时间:2018-12-05 14:35:22

标签: javascript java websocket

我有一个后端:

SessionEndpoint:

@ServerEndpoint("/session")
public class SessionEndpoint {

    private static Set<SessionEndpoint> sessionEndpoints = new CopyOnWriteArraySet<>();

    @OnMessage
    public void onMessage(Session session, String sessionId) {
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("sessionId", sessionId);

        sessionEndpoints.forEach(endpoint -> {
            synchronized (endpoint) {
                try {
                    session.getBasicRemote().sendObject(attributes);
                } catch (IOException | EncodeException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

还有一个客户:

let webSocket = new WebSocket('ws://localhost:9999/session');
webSocket.onopen = () => webSocket.send('hello');

webSocket.onmessage = function(response) {
    console.log(response);
};

当我websocket尝试建立连接时,结果是404响应。我应该如何联系后端?

0 个答案:

没有答案