2

在 JSR-356 中广播服务器发起的 WebSocket 消息的最佳实践是什么?

澄清一下,我知道使用注释时回复甚至广播是如何工作的@OnMessage,但我想从服务器发送一个事件,而不先从客户端接收消息。换句话说,我想我需要在MessageServerEndpoint下面的代码中引用实例。

我见过以下解决方案,但它使用静态方法并且不是很优雅。

@ServerEndpoint(value = "/echo")
public class MessageServerEndpoint {
    private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());

    @OnOpen
    public void onOpen(Session session) {
        sessions.add(session);
    }

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

    // Static method - I don't like this at all
    public static void broadcast(String message) {
        for (Session session : sessions) {
            if (session.isOpen()) {
                session.getBasicRemote().sendText(message);
            }
        }
    }
}

public class OtherClass {
    void sendEvent() {
        MessageServerEndpoint.broadcast("test");
        // How do I get a reference to the MessageServerEndpoint instance here instead?
    }
}
4

1 回答 1

1

我通过扩展ServerEndpointConfig.Configurator和覆盖getEndpointInstance()可以保存端点实例的位置解决了这个问题:

public class MyEndpointConfigurator extends ServerEndpointConfig.Configurator
    private Set<MyEndpoint> endpoints = Collections.synchronizedSet(new HashSet<>());

    @Override
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
        try {
            T endpoint = endpointClass.newInstance();
            MyEndpoint myEndpoint = (MyEndpoint) endpoint;
            myEndpoint.setConfigurator(this);
            endpoints.add(myEndpoint);
            return endpoint;
        } catch (IllegalAccessException e) {
            throw new InstantiationException(e.getMessage());
        }
    }

    // Call this from MyEndpoint.onClose()
    public void removeInstance(MyEndpoint endpoint) {
        endpoints.remove(endpoint);
    }
}

因为我有对 的引用MyEndpointConfigurator,所以我也有对所有端点的引用。

它仍然感觉像一个黑客,但似乎可以解决问题。

于 2014-08-15T07:05:32.820 回答