Java 从服务器向websocket客户端发送消息

Java 从服务器向websocket客户端发送消息,java,websocket,glassfish,Java,Websocket,Glassfish,我正在使用glassfish构建一个websocket应用程序,在给定事件中,我需要我的服务器向所有连接的客户端发送消息。我可以从两者发送和接收消息,但我不能使用类服务器发送消息 我的服务器类具有以下主体: @ApplicationScoped @ServerEndpoint(“/actions”) 公共类设备WebSocketServer{ @Inject private DeviceSessionHandler sessionHandler; @OnOpen public void ope

我正在使用glassfish构建一个websocket应用程序,在给定事件中,我需要我的服务器向所有连接的客户端发送消息。我可以从两者发送和接收消息,但我不能使用类服务器发送消息

我的服务器类具有以下主体:

@ApplicationScoped
@ServerEndpoint(“/actions”)
公共类设备WebSocketServer{

@Inject
private DeviceSessionHandler sessionHandler;

@OnOpen
public void open(Session session) {
    sessionHandler.addSession(session);
}

@OnClose
public void close(Session session) {
    sessionHandler.removeSession(session);
}

@OnError
public void onError(Throwable error) {
    Logger.getLogger(DeviceWebSocketServer.class.getName()).log(Level.SEVERE, null, error);
}

@OnMessage
public void handleMessage(String message, Session session) {

    System.out.println("Chegou uma mensagem: " + message);
    System.out.println("Na sessao: " + session.getId());

    try (JsonReader reader = Json.createReader(new StringReader(message))) {
        JsonObject jsonMessage = reader.readObject();

        if ("add".equals(jsonMessage.getString("action"))) {
            Device device = new Device();
            device.setName(jsonMessage.getString("name"));
            device.setDescription(jsonMessage.getString("description"));
            device.setType(jsonMessage.getString("type"));
            device.setStatus("Off");
            sessionHandler.addDevice(device);
        }

        if ("remove".equals(jsonMessage.getString("action"))) {
            int id = (int) jsonMessage.getInt("id");
            sessionHandler.removeDevice(id);
        }

        if ("toggle".equals(jsonMessage.getString("action"))) {
            int id = (int) jsonMessage.getInt("id");
            sessionHandler.toggleDevice(id);
        }
    }

}

收到事件后如何向客户发送消息?是否应实例化我的class server?

此白板应用程序中有一个关于如何向所有连接的客户端发送消息的示例:

@ServerEndpoint(value=“/whiteboardendpoint”,编码器={FigureEncoder.class},解码器={FigureDecoder.class})
公共类MyWhiteboard{
private static Set peers=Collections.synchronizedSet(new HashSet());
@OnMessage
public void broadcastFigure(Figure Figure,Session Session)抛出IOException,EncodeException{
System.out.println(“广播数字:+数字);
用于(会话对等方:对等方){
如果(!peer.equals(会话)){
peer.getBasicRemote().sendObject(图);
}
}
}

关于如何在此白板应用程序中向所有连接的客户端发送消息,有一个示例:

@ServerEndpoint(value=“/whiteboardendpoint”,编码器={FigureEncoder.class},解码器={FigureDecoder.class})
公共类MyWhiteboard{
private static Set peers=Collections.synchronizedSet(new HashSet());
@OnMessage
public void broadcastFigure(Figure Figure,Session Session)抛出IOException,EncodeException{
System.out.println(“广播数字:+数字);
用于(会话对等方:对等方){
如果(!peer.equals(会话)){
peer.getBasicRemote().sendObject(图);
}
}
}
@ServerEndpoint(value="/whiteboardendpoint", encoders = {FigureEncoder.class}, decoders = {FigureDecoder.class})
public class MyWhiteboard {

    private static Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());

   @OnMessage
   public void broadcastFigure(Figure figure, Session session) throws IOException, EncodeException {
    System.out.println("broadcastFigure: " + figure);
    for (Session peer : peers) {
        if (!peer.equals(session)) {
            peer.getBasicRemote().sendObject(figure);
        }
    }
}