当前位置:网站首页>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
边栏推荐
- January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
- Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
- X509 certificate cer format to PEM format
- Vscode Chinese plug-in doesn't work. Problem solving
- 8.3 language model and data set
- Adobe Illustrator menu in Chinese and English
- Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
- 分享 20 个不容错过的 ES6 的技巧
- Is asemi ultrafast recovery diode interchangeable with Schottky diode
- 【thymeleaf】处理空值和使用安全操作符
猜你喜欢
X509 certificate cer format to PEM format
分享 20 个不容错过的 ES6 的技巧
Explanation and example application of the principle of logistic regression in machine learning
Daily question - leetcode396 - rotation function - recursion
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
LeetCode149-直线上最多的点数-数学-哈希表
Have you learned the basic operation of circular queue?
Five data types of redis
Kubernetes详解(十一)——标签与标签选择器
随机推荐
Practice of unified storage technology of oppo data Lake
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
Will golang share data with fragment append
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
The difference between having and where in SQL
Modify the default listening IP of firebase emulators
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
The life cycle of key value in redis module programming
Llvm - generate local variables
分享3个使用工具,在家剪辑5个作品挣了400多
JUC learning record (2022.4.22)
22年了你还不知道文件包含漏洞?
Analysis of common storage types and FTP active and passive modes
X509 certificate cer format to PEM format
Nacos程序连接MySQL8.0+ NullPointerException
win10 任务栏通知区图标不见了
API gateway / API gateway (II) - use of Kong - load balancing
A series of problems about the best time to buy and sell stocks
Async void caused the program to crash
Brute force of DVWA low -- > High