如何使用 DWR 更新用户列表
我正在开发可以连接到 Gtalk 和 Facebook 的聊天客户端。我正在使用 DWR 来实现此目的。
登录后,我必须填充用户列表。在客户端我有
function showUsersOnline() {
var cellFuncs = [ function(user) {
return '<i>'+user+'</i>';
} ];
LoginG.usersOnline( {
callback : function(users) {
dwr.util.removeAllRows('usersOnline');
dwr.util.addRows("usersOnline", users, cellFuncs, {
escapeHtml : false
});
在服务器端我使用 Smack Api 来获取名册列表(在线)
public void usersOnline() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println(roster.getEntryCount());
int count1 = 0;
int count2 = 0;
for (RosterEntry r : entries) {
String user = r.getUser();
Presence presence = roster.getPresence(user);
if (presence.getType() == Presence.Type.available) {
System.out.println(user + " is online");
count1++;
} else {
System.out.println(user + " is offline");
count2++;
}
现在我应该将数据作为 JSON 返回还是有一种方法 DWR 可以处理集合???
I am developing chat client which can connect to Gtalk and Facebook.I am using DWR for the purpose.
Once I log into I have to populate the user s lists. On client side I have
function showUsersOnline() {
var cellFuncs = [ function(user) {
return '<i>'+user+'</i>';
} ];
LoginG.usersOnline( {
callback : function(users) {
dwr.util.removeAllRows('usersOnline');
dwr.util.addRows("usersOnline", users, cellFuncs, {
escapeHtml : false
});
On server side I am using Smack Api to get the roster list(online)
public void usersOnline() {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println(roster.getEntryCount());
int count1 = 0;
int count2 = 0;
for (RosterEntry r : entries) {
String user = r.getUser();
Presence presence = roster.getPresence(user);
if (presence.getType() == Presence.Type.available) {
System.out.println(user + " is online");
count1++;
} else {
System.out.println(user + " is offline");
count2++;
}
Now should I return the data as JSON or is there a way DWR can handle the collection???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您修改服务器方法
usersOnline()
以返回Collection
对象,则 DWR 会将其填充到回调函数的参数中,在您的情况下为功能(用户)
。因此,在调用返回到回调函数function(users)
后,您可以通过users
对象来获取服务器端方法对其进行的更新。users
对象需要像array
一样进行遍历,因为您要返回一个Collection
或List
无论适用什么。这是您要找的吗?有关此内容的更多信息,请参阅此处。
If you modify your server method
usersOnline()
to return theCollection<RosterEntry>
object then DWR will populate that in the argument of the callback function which in your case isfunction(users)
. So after the call is returned back to the callback functionfunction(users)
you can go through theusers
object to get the updates made to it by the server side method. Theusers
object will need to be traversed like anarray
since you are returning aCollection
or aList
whatever applies.Is this what you are looking for? More on this can be read here.