Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

批量发送concept.send(..) #41

Closed
a386572631 opened this issue May 15, 2024 · 1 comment
Closed

批量发送concept.send(..) #41

a386572631 opened this issue May 15, 2024 · 1 comment

Comments

@a386572631
Copy link

依赖名称:

com.github.linyuzai
concept-websocket-loadbalance-spring-boot-starter
2.5.0

依赖版本:
2.5.0

问题描述:
批量发送的时候,concept.send(..)的消息体每个人收到都是一样的,但应该是需要不一样的,因为需要额外在消息体内放一个类似消息记录编号的属性,用来做已读回执。

异常堆栈:

代码示例:

@Linyuzai
Copy link
Owner

2.6.0的版本可以通过下面的方式修改下发的消息,可以试一下

public void sendEntity(Entity entity) {
    //这里需要指定Class用于数据转发之后的反序列化
    ObjectMessage message = new ObjectMessage(entity, Entity.class);
    concept.send(message);
}

//注入一个编解码适配器给每个消息设置不同的数据
@Component
public class WsEntityCodecAdapter extends WebSocketMessageCodecAdapter {

    @Override
    public MessageEncoder getClientMessageEncoder(MessageEncoder encoder) {
        //自定义编码器
        return new EntityMessageEncoder(encoder);
    }

    @RequiredArgsConstructor
    public static class EntityMessageEncoder implements MessageEncoder {

        private final MessageEncoder encoder;

        @Override
        public Object encode(Message message, Connection connection, ConnectionLoadBalanceConcept concept) {
            Object payload = message.getPayload();
            //如果是实体对象
            if (payload instanceof Entity) {
                //新建一个实体对象设置内容以及编号
                Entity newEntity = new Entity();
                newEntity.setContent(((Entity) payload).getContent());
                newEntity.setId(UUID.randomUUID().toString());
                //新建一个消息对象设置消息头和新实体对象
                ObjectMessage newMessage = new ObjectMessage();
                newMessage.getHeaders().putAll(message.getHeaders());
                newMessage.setPayload(newEntity);
                //传入新消息对象
                return encoder.encode(newMessage, connection, concept);
            }
            return encoder.encode(message, connection, concept);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants