mrniko / netty-socketio

Socket.IO server implemented on Java. Realtime java framework
Apache License 2.0
6.77k stars 1.65k forks source link

how to use redisson to resolve distribution system? #775

Open silloy opened 4 years ago

silloy commented 4 years ago

I hava two server use same redis instance, server1 and server2, client has connect to server1, how Can I send message to client from server2?

server2 code:

    String sessionKey = uid;
    RBucket<UUID> rb = redissonClient.getBucket(sessionKey);
    UUID sessionId = rb.get();
    LOGGER.info("user uid={}, uuid={}", uid, sessionId);
    SocketIOClient client = socketIoServer.getClient(sessionId);

client is none, I want to know why? I have use the redis

merzMario commented 3 years ago

How to use redisson:

@Component
@Slf4j
public class ServerRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {
    private SocketIOServer server;
    @Autowired
    private RedissonStoreFactory redissonStoreFactory;
    @Bean
    public SocketIOServer socketIOServer() {
        Configuration config = new Configuration();
        config.setPort("9090");
        config.setStoreFactory(redissonStoreFactory);
        server = new SocketIOServer(config);
        return server;
    }
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }
    @Override
    public void run(String... args) {
        server.start();
    }
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        server.stop();
    }
}

Then let clients join the same room on different JVMs.

client1.joinRoom("roomName");
client2.joinRoom("roomName");

When you send message,you can use the sendEvent() method:

server.getRoomOperations("roomName").sendEvent("eventName",message);

The client will subscribe the dispatch event ,and when you invoke the sendEvent() method ,netty-socketio will publish a dispatch event,detials in BroadcastOperation#dispatch(Packet packet).

I hope this can help you .

babizhu commented 9 months ago

How to use redisson:

@Component
@Slf4j
public class ServerRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {
    private SocketIOServer server;
    @Autowired
    private RedissonStoreFactory redissonStoreFactory;
    @Bean
    public SocketIOServer socketIOServer() {
        Configuration config = new Configuration();
        config.setPort("9090");
        config.setStoreFactory(redissonStoreFactory);
        server = new SocketIOServer(config);
        return server;
    }
    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }
    @Override
    public void run(String... args) {
        server.start();
    }
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        server.stop();
    }
}

Then let clients join the same room on different JVMs.

client1.joinRoom("roomName");
client2.joinRoom("roomName");

When you send message,you can use the sendEvent() method:

server.getRoomOperations("roomName").sendEvent("eventName",message);

The client will subscribe the dispatch event ,and when you invoke the sendEvent() method ,netty-socketio will publish a dispatch event,detials in BroadcastOperation#dispatch(Packet packet).

I hope this can help you .

server.getRoomOperations("roomName").sendEvent("eventName",message);

how to receive this message?