当前位置:网站首页>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
边栏推荐
- Flink datastream type system typeinformation
- Explanation and example application of the principle of logistic regression in machine learning
- Lotus DB design and Implementation - 1 Basic Concepts
- Reptile exercises (1)
- 中富金石财富班29800效果如何?与专业投资者同行让投资更简单
- 小红书 timestamp2 (2022/04/22)
- How does eolink help telecommuting
- MySQL Basics
- 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
- Thinkphp5 + data large screen display effect
猜你喜欢
8.2 text preprocessing
Advanced version of array simulation queue - ring queue (real queuing)
Tun model of flannel principle
Introduction to distributed transaction Seata
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
重定向和请求转发详解
深度学习——超参数设置
eolink 如何助力遠程辦公
Tun equipment principle
Have you learned the basic operation of circular queue?
随机推荐
Tun model of flannel principle
js——实现点击复制功能
How does eolink help telecommuting
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
How to upload large files quickly?
Llvm - generate for loop
机器学习——逻辑回归
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
MySQL Basics
T2 iCloud日历无法同步
Baidu written test 2022.4.12 + programming topic: simple integer problem
Modify the default listening IP of firebase emulators
Leetcode162 - find peak - dichotomy - array
Byte interview programming question: the minimum number of K
LeetCode167-两数之和II-双指针-二分-数组-查找
Reptile exercises (1)
redis-shake 使用中遇到的错误整理
Compiling OpenSSL
Share 20 tips for ES6 that should not be missed