当前位置:网站首页>Wechat applet customer service access to send and receive messages
Wechat applet customer service access to send and receive messages
2022-04-23 15:14:00 【A run】
The company has many small programs and customer service functions , Realize unified access to chat
The notification address of each applet notification access is different , But the logic of the access process is the same , Identify the applet when accessing
netty Send a message to the page uniformly websocket part , Define the message body required by your business
@Component
public class ChannelWechatNotice {
private static final Logger lg = LoggerFactory.getLogger(ChannelWechatNotice.class);
/**socketWxchat.put("message", JzWxchat);
* socketWxchat.put("type", "weixin");
* @param socketWxchat
*/
@Async
public void postSocketUser(Map<String,Object> socketWxchat) {
String jsonMsg=JsonUtil.getJsonLocalDataTime(socketWxchat);
// String jsonMsg=JSONUtils.toJSONString(socketWxchat);
lg.info(" call netty Send a message message :" + jsonMsg);
try {
// Send... To the specified user
// ChannelUser channelUser = ChannelConst.getChannelUser("1512312");
// if(channelUser==null) {
// lg.info(" Wechat notification call netty Send a message to userId:" + null);
// return;
// }
// Send to all online users
Map<String, ChannelUser> channelMap = ChannelConst.channelMap;
for (Entry<String, ChannelUser> s : channelMap.entrySet()) {
Channel channel = s.getValue().getChannel();
channel.writeAndFlush(new TextWebSocketFrame(jsonMsg));
}
} catch (Exception e) {
e.printStackTrace();
}
}
netty Integration part ,
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private static final Logger logger = LoggerFactory.getLogger(ChatHandler.class);
/**
* Broadcast to get messages
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (null != msg && msg instanceof FullHttpRequest) {
// You have to come here for the first time , Think the link here is successful
FullHttpRequest request = (FullHttpRequest) msg;
String uri = request.uri();
Map paramMap = getUrlParams(uri);
// Identification and sysuser Do the processing
System.out.println("channelRead The parameters of the receiving connection are :" + JsonUtil.getJson(paramMap));
if (uri.contains("?")&¶mMap.containsKey(ChannelConst.CONST_USERID)) {
String userId = paramMap.get(ChannelConst.CONST_USERID).toString();
String channelShortId = getChannelShortId(ctx);
ChannelUser cu = new ChannelUser(channelShortId, userId, ctx.channel());
ChannelConst.channelMap.put(userId, cu);
System.err.println(" Identification succeeded , client channelMap Number :" + ChannelConst.clients.size());
}else {
ctx.close();// Don't let him connect
System.err.println(" Remove the abnormal connection ===========================================");
return ;
}
String newUri = uri.substring(0, uri.indexOf("?"));
request.setUri(newUri);
}
if(msg instanceof TextWebSocketFrame) {
TextWebSocketFrame frame = (TextWebSocketFrame) msg;
System.out.println(" Received a message from the client :" + frame.text());
// Send the received message to all clients
for (Channel channel : ChannelConst.clients) {
// Pay attention to all websocket All data should be in the form of TextWebSocketFrame encapsulate
channel.writeAndFlush(
new TextWebSocketFrame(" Messages received by the server :" + LocalDateTime.now() + ", The news is :" + frame.text()));
}
}
// if(msg instanceof CloseWebSocketFrame) {
// logger.info(" There are users who have exited ===========================================");
// }else {
// logger.info(" There's other news ===========================================");
// }
super.channelRead(ctx, msg);
}
High imitation wechat interface , You can send pictures or text , Different customers are connected to chat by different customer service
版权声明
本文为[A run]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407312891.html
边栏推荐
- Leetcode151 - invert words in string - String - simulation
- Have you really learned the operation of sequence table?
- Flink datastream type system typeinformation
- Llvm - generate if else and pH
- Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
- Little red book timestamp2 (2022 / 04 / 22)
- Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
- 如何设计一个良好的API接口?
- Educational Codeforces Round 127 A-E题解
- HJ31 单词倒排
猜你喜欢
About UDP receiving ICMP port unreachable
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
Kubernetes详解(九)——资源配置清单创建Pod实战
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
How to use OCR in 5 minutes
LeetCode162-寻找峰值-二分-数组
Tun model of flannel principle
How does eolink help telecommuting
每日一题-LeetCode396-旋转函数-递推
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
随机推荐
For 22 years, you didn't know the file contained vulnerabilities?
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
The win10 taskbar notification area icon is missing
UML learning_ Day2
C language super complete learning route (collection allows you to avoid detours)
8.3 language model and data set
How to design a good API interface?
Thinkphp5 + data large screen display effect
Unity_ Code mode add binding button click event
Nuxt project: Global get process Env information
大文件如何快速上传?
JUC learning record (2022.4.22)
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
js——實現點擊複制功能
Common interview questions of operating system:
分享3个使用工具,在家剪辑5个作品挣了400多
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Leetcode exercise - 396 Rotation function
The difference between having and where in SQL
填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]