当前位置:网站首页>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
边栏推荐
- API gateway / API gateway (II) - use of Kong - load balancing
- My raspberry PI zero 2W toss notes to record some problems and solutions
- LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
- Basic operation of sequential stack
- C language super complete learning route (collection allows you to avoid detours)
- Have you really learned the operation of sequence table?
- Redis master-slave synchronization
- How to upload large files quickly?
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- Brute force of DVWA low -- > High
猜你喜欢
How to use OCR in 5 minutes
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Leetcode162 - find peak - dichotomy - array
重定向和请求转发详解
The wechat applet optimizes the native request through the promise of ES6
Analysis of common storage types and FTP active and passive modes
Tun equipment principle
Openfaas practice 4: template operation
Explanation and example application of the principle of logistic regression in machine learning
随机推荐
分布式事务Seata介绍
X509 certificate cer format to PEM format
牛客网数据库SQL实战详细剖析(26-30)
Tun model of flannel principle
Reptile exercises (1)
MySQL Basics
How to use OCR in 5 minutes
1n5408-asemi rectifier diode
免费在upic中设置OneDrive或Google Drive作为图床
Byte interview programming question: the minimum number of K
Compiling OpenSSL
Async keyword
大文件如何快速上传?
机器学习——逻辑回归
Error: unable to find remote key "17f718f726"“
Llvm - generate for loop
Leetcode149 - maximum number of points on a line - Math - hash table
SQLSERVER事物与锁的问题
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
Differential privacy (background)