import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Future;
import javax.websocket.Session;
import org.springframework.stereotype.Service;
@Service("webSocketService")
public class WebSocketService {
// 记录空闲Session集合
private static CopyOnWriteArraySet<Session> idle = new CopyOnWriteArraySet<Session>();
// 记录正在使用中Session集合,value为Future,表示使用情况
private static ConcurrentHashMap<Session, Future<Void>> busy = new ConcurrentHashMap<Session, Future<Void>>();
// 新增Session
public static void open(Session session) {
idle.add(session);
}
// 关闭Session
public static void close(Session session) {
idle.remove(session);
busy.remove(session);
}
// 使用session发送消息
public static void send(Session session, Object message, Integer timeout) throws InterruptedException {
if (timeout < 0) { // timeout后放弃本次发送
return;
}
if (idle.remove(session)) { // 判断session是否空闲,抢占式
if (message instanceof String) {
busy.put(session, session.getAsyncRemote().sendText((String) message));
} else if (message instanceof ByteBuffer) {
busy.put(session, session.getAsyncRemote().sendBinary((ByteBuffer) message));
} else { // 消息类型有问题时,不发送。目前只添加了两种消息类型的发送方式,之后有需求可以扩展
idle.add(session);
}
} else {
// 若session当前不在idle集合,则去busy集合中查看session上次是否已经发送完毕,即采用惰性判断
synchronized (busy) {
if (busy.containsKey(session) && busy.get(session).isDone()) {
busy.remove(session);
idle.add(session);
}
}
// 重试
Thread.sleep(100);
send(session, message, timeout - 100);
}
}
}
使用:
WebSocketService.send(session, "content", 3000);
转发:The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method的问题在于:handlerA和handlerB两个方法有可能同时执行,当A或者B方法遍历到某一个session并且调用sendMessage发送消息的时候,另外一个方法也正好也在使用相同的session发送另外一个消息(同一个session消息发送冲突了,也就是说同一个时刻,多个线程向一个socket写数据冲突了),就会报TEXT_FULL_WRITING异常。
一般采用的解决方案是使用同步锁加同步发送(session.getBasicRemote())的方式,但很多时候需要异步发送。
在我的项目中,采用Tomcat,TEXT_FULL_WRITING会直接导致websocket连接断掉,采用这种解决方案能保证一个session同时只会在发送一条消息,所以避免了TEXT_FULL_WRITING错误。
这种解决方案只是利用一种缓冲加惰性判断的方式,并不是标准解决方案,如果觉得这种方案有什么问题,或者有更好的方案,可以大家讨论下。