多服务器消息推送消息,多浏览器窗口接收websocket服务器推送消息问题

问题已经解决,通过抓包发现websocket连接及发送均为正常,感谢版主xmt1139057136提供思路问题出在服务器端内容发送环节,因为不太了解websocket发送机制

问题已经解决,通过抓包发现websocket连接及发送均为正常,感谢版主xmt1139057136提供思路

问题出在服务器端内容发送环节,因为不太了解websocket发送机制,这个错误看起来有点犯2

下面贴出代码,希望出现同样问题的各位有个参考,

服务器端使用的是tomcat7.0以上版本websocket

public void sendMsg(String msg) {

StreamInbound streamInbound = null;

//此处为错误代码,问题出现原因是在for循环外调用了wrap方法,结果在发送后buffer自动被清空

//但是不会引起程序的异常,所以看起来就像我提到的那个现象,略坑。。。

//CharBuffer buffer = CharBuffer.wrap(msg);

for(int i=0;i

// streamInbounds记录的是当前进行连接的客户端个数

// 用以确保每个客户端都能接受服务器发送的消息

// 声明方式为 public static Vector streamInbounds = new Vector();

streamInbound = streamInbounds.get(i);

try {

// 将wrap方法移动到for循环内,每次都重新进行赋值,问题解决

CharBuffer buffer = CharBuffer.wrap(msg);

streamInbound.getWsOutbound().writeTextMessage(buffer);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

再次感谢版主xmt1139057136

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755022992a5227624.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信