大气websocket返回多个对象

时间:2018-11-26 09:44:14

标签: java websocket atmosphere

我正在使用用于Websocket的Atmosphere框架,但我不理解我从Atmosphere获得的响应,也许有人可以为我指出正确的方向。

从本质上讲,我有几种类型的用户发送到服务器Websocket的消息,每种类型都定义为带参数“ type”的MessageBean,以使用Google Gson RuntimeTypeAdapterFactory类区分每种消息类型。

这一切都很好,但是问题是当我将消息编码回用户时:

@Message(decoders = {MessageServer.class}, encoders = {MessageServer.class})
public MessageProtocolBean onMessage(AtmosphereResource atmosphereResource,      
    MessageBean message) throws IOException {
    if (message instanceof ChatBean) {
       ... do stuff
       return new MessgeProtocolBean(chatBean);
    } else {
    ...
    }

})

现在MessageProtocolBean可以类似于以下内容:

public class MessageProtocolBean {

    private String message;
    private ChatBean chat = new ChatBean(); 
    private List<ChatBean> chats = new ArrayList<ChatBean>();
    private List<User> users = new ArrayList<User>();
    private List<AlertBean> alerts = new ArrayList<AlertBean>();
    private Map<String,ContactBean> contacts = new 
           HashMap<String,ContactBean>();

    public MessageProtocolBean(ChatBean chat) {
            this.chat = chat;
    }

    public MessageProtocolBean(ChatBean chat, Collection<User> users, 
       Map<String,ContactBean> contacts, Collection<AlertBean> alerts) {
            this.chat = chat;
            this.users.addAll(users);
            this.contacts.putAll(contacts);
            this.alerts.addAll(alerts);
    }

现在,当用户仅发送聊天消息时,返回给所有客户端的对象是JavaScript对象:

Object { chat: {…}, chats: [], users: [], alerts: [], contacts: {} }

请注意,“ chat”参数不为空,但其他参数均为​​空,尽管事实上仅返回了一条聊天消息。我不确定的是,尽管我已指定return MessageProtocolBean(ChatBean chat),但为什么我会得到全部?

0 个答案:

没有答案