Code Monkey home page Code Monkey logo

Comments (1)

Linyuzai avatar Linyuzai commented on June 26, 2024

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);
        }
    }
}

from concept.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.