我正在使用用于 websockets 的 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: {} }
请注意,“聊天”参数不是空的,但所有其他参数都是空的,尽管实际上只返回了一条聊天消息。我不确定的是,虽然我指定了返回 MessageProtocolBean(ChatBean chat),但为什么我得到了所有?