当前位置:网站首页>[TiO websocket] III. The TiO websocket server can send messages to users anywhere

[TiO websocket] III. The TiO websocket server can send messages to users anywhere

2022-06-11 09:20:00 Asurplus、

In the last article , We have successfully built our websocket The server , And can successfully communicate with the server , This article will lead you to realize that you can send messages to users anywhere

1、 Principle analysis

tio-websocket-server The method of sending messages to users in is as follows :

Tio.sendToUser(channelContext.tioConfig, userId, wsResponse);
  • channelContext.tioConfig, Refer to the whole websocket Configuration information , Can pass ChannelContext.tioConfig obtain
  • userId: The receiver id
  • wsResponse: The response object , Be similar to httpServletResponse

We are in the message processing class MyWsMsgHandler Easy to get in ChannelContext object , We can statically call Tio Various api 了

however , If not in Message processing class MyWsMsgHandler in , We won't get ChannelContext object , We need to be in websocket In the configuration class ServerTioConfig Defined as a global variable , So we can send messages anywhere

2、 reform websocket Configuration class

import com.asurplus.tio.websocket.handle.MyWsMsgHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tio.server.ServerTioConfig;
import org.tio.websocket.server.WsServerStarter;

import java.io.IOException;

/** * websocket  Configuration class  */
@Configuration
public class WebSocketConfig {
    

    /** *  Injection message processor  */
    @Autowired
    private MyWsMsgHandler myWsMsgHandler;

    /** * TIO-WEBSOCKET  Configuration information  */
    public static ServerTioConfig serverTioConfig;

    /** *  Start class configuration  * * @return * @throws IOException */
    @Bean
    public WsServerStarter wsServerStarter() throws IOException {
    
        //  Set up the processor 
        WsServerStarter wsServerStarter = new WsServerStarter(6789, myWsMsgHandler);
        //  Get ServerTioConfig
        serverTioConfig = wsServerStarter.getServerTioConfig();
        //  Set heartbeat timeout , Default :1000 * 120
        serverTioConfig.setHeartbeatTimeout(1000 * 120);
        //  start-up 
        wsServerStarter.start();
        return wsServerStarter;
    }
}

such , We define a global variable serverTioConfig, Where needed , It only needs WebSocketConfig.serverTioConfig Can get

3、 Use demonstration

Tio.sendToUser(WebSocketConfig.serverTioConfig, "1", wsResponse);
Tio.sendToGroup(WebSocketConfig.serverTioConfig, "1234", wsResponse);

So we can get it anywhere websocket Configuration information , For users perhaps group Sent a message

If you find deficiencies in reading , Welcome to leave a message !!!

原网站

版权声明
本文为[Asurplus、]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020507002523.html