websocket客户端向服务器发送信息,websocket从服务器向所有客户端发送消息

@OnMessage

public void onMessage(String message, Session session) {

switch (message) {

case "latencyEqualize":

for (Session otherSession : session.getOpenSessions()) {

RemoteEndpoint.Basic other = otherSession.getBasicRemote();

String data = "Max latency = "

+ LatencyEqualizer.getMaxLatency(latencies);

try {

other.sendText(data);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

break;

default:

RemoteEndpoint.Basic other = session.getBasicRemote();

try {

other.sendText(message);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

}

此代码有问题。当我从第一个客户端发送消息“latencyEqualize”时,服务器仅对同一个客户端进行响应。其他客户端不会收到消息“最大延迟= 15”。但是当第二个客户端向服务器发送任何消息时,他会收到“Max latency = 15”。所有未来的服务器调用都会返回以前调用的消息。

有没有办法避免这种情况。我希望当其中一个客户端向服务器发送“latencyEqualize”消息时,所有客户端都会收到“Max latency”消息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值