问题已经解决,通过抓包发现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