comet4j取不到map值

黄啊码 2013-12-15 05:28:16
package com.sec.comet4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.comet4j.core.CometConnection;
import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
import org.comet4j.core.event.ConnectEvent;
import org.comet4j.core.listener.ConnectListener;

public class HelloWorld extends ConnectListener implements
ServletContextListener {
private static final String CHANNEL = "hello";
Map<String, String> map = new HashMap<String, String>();;
CometContext cometContext = CometContext.getInstance();

public void contextInitialized(ServletContextEvent arg0) {
CometContext cc = CometContext.getInstance();
cc.registChannel(CHANNEL);
Thread helloAppModule = new Thread(new HelloAppModule(),
"Sender App Module");
helloAppModule.setDaemon(true);
CometEngine engine = cometContext.getEngine();
engine.addConnectListener(new HelloWorld());
helloAppModule.start();

}

class HelloAppModule implements Runnable {
CometEngine engine = cometContext.getEngine();

public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception ex) {
ex.printStackTrace();
}
String out = (String) cometContext.getServletContext()
.getAttribute("out");
List<CometConnection> connections = engine.getConnections();
System.out.println(map.size() + "集合的大小");
engine.sendTo(CHANNEL, connections, "");
engine.sendToAll(CHANNEL, out);
if (connections.size() >= 1) {
cometContext.getServletContext().removeAttribute("out");
}
}
}
}

public void contextDestroyed(ServletContextEvent arg0) {
}

public boolean handleEvent(ConnectEvent arg0) {
System.out.println("进入监听器");
// TODO Auto-generated method stub
System.out.println(cometContext.getServletContext().getAttribute(
"usersName"));
System.out.println(arg0.getConn().getId());
map.put((String) cometContext.getServletContext().getAttribute(
"usersName"), arg0.getConn().getId());
(这里在后台已经拿到值了,在这也put进去了,但是在上边判断集合大小时总是为0,为啥呢)
return true;
}

}
...全文
35 点赞 收藏 回复
写回复
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复

还没有回复,快来抢沙发~

相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2013-12-15 05:28
社区公告
暂无公告