tio-websocket-server 中对用户发送消息的方法如下:
Tio.sendToUser(channelContext.tioConfig, userId, wsResponse);
•
channelContext.tioConfig,指整个 websocket 的配置信息,可以通过 ChannelContext.tioConfig 获取
• userId:接收者 id
• wsResponse:响应对象,类似于 httpServletResponse
我们在消息处理类 MyWsMsgHandler 中很轻松获得 ChannelContext 对象,我们就可以静态调用 Tio 的各种 api 了
但是,如果不是在 消息处理类 MyWsMsgHandler 中,我们就获取不到 ChannelContext 对象,我们就需要在 websocket 配置类中将 ServerTioConfig 定义为全局变量,这样我们在任何地方都能实现发送消息了
方法一:
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
public class WebSocketConfig {
/**
* 注入消息处理器
*/
@Autowired
private MyWsMsgHandler myWsMsgHandler;
/**
* TIO-WEBSOCKET 配置信息
*/
public static ServerTioConfig serverTioConfig;
/**
* 启动类配置
*
* @return
* @throws IOException
*/
@Bean
public WsServerStarter wsServerStarter() throws IOException {
// 设置处理器
WsServerStarter wsServerStarter = new WsServerStarter(6789, myWsMsgHandler);
// 获取到ServerTioConfig
serverTioConfig = wsServerStarter.getServerTioConfig();
// 设置心跳超时时间,默认:1000 * 120
serverTioConfig.setHeartbeatTimeout(1000 * 120);
// 启动
wsServerStarter.start();
return wsServerStarter;
}
}
这样,我们就定义了一个全局变量 serverTioConfig,在需要使用的地方,只需要 WebSocketConfig.serverTioConfig 就能拿到
方法二:
package com.geostar.dvp.websocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.server.ServerTioConfig;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.server.handler.IWsMsgHandler;
/**
* @author Administrator
*/
@Slf4j
@Component
public class WebSocketMessageHandler implements IWsMsgHandler {
/**
* TIO-WEBSOCKET 配置信息
*/
public static TioConfig serverTioConfig;
@Override
public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
serverTioConfig = channelContext.tioConfig;
return httpResponse;
}
@Override
public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
// 拿到用户id
String id = httpRequest.getParam("id");
// 绑定用户
Tio.bindUser(channelContext, id);
// 绑定业务类型(根据业务类型判定处理相关业务)
String bsId = httpRequest.getParam("bsId");
if (StringUtils.isNotBlank(bsId)){
Tio.bindBsId(channelContext, bsId);
}
// 给用户发送消息
WsResponse wsResponse = WsResponse.fromText("您已成功连接 WebSocket 服务器", "UTF-8");
Tio.sendToUser(channelContext.tioConfig, id, wsResponse);
}
@Override
public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
return null;
}
@Override
public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
// 关闭连接
Tio.remove(channelContext, "WebSocket Close");
return null;
}
@Override
public Object onText(WsRequest wsRequest, String s, ChannelContext channelContext) throws Exception {
WsResponse wsResponse = WsResponse.fromText("服务器已收到消息:" + s, "UTF-8");
Tio.sendToUser(channelContext.tioConfig, userid, wsResponse);
return null;
}
}
创建静态变量,这样在初次握手时就能获取serverTioConfig。
方法一:
Tio.sendToUser(WebSocketConfig.serverTioConfig, "1", wsResponse);
Tio.sendToGroup(WebSocketConfig.serverTioConfig, "1234", wsResponse);
方法二:
TioConfig serverTioConfig = WebSocketMessageHandler.serverTioConfig;
WsResponse wsResponse = WsResponse.fromText("服务器已收到消息:", "UTF-8");
Tio.sendToUser(serverTioConfig, "id", wsResponse);
这样我们就能在任何地方拿到 websocket 配置信息,对用户 或者 群组 发送消息了