对于Websocket发送消息时,报错“TEXT_FULL_WRITING”的一种解决方案

13 篇文章 0 订阅
3 篇文章 0 订阅
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错误。

这种解决方案只是利用一种缓冲加惰性判断的方式,并不是标准解决方案,如果觉得这种方案有什么问题,或者有更好的方案,可以大家讨论下。

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值