当前位置:网站首页>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
边栏推荐
- 1n5408-asemi rectifier diode
- Leetcode151 - invert words in string - String - simulation
- Three uses of kprobe
- Async keyword
- T2 iCloud日历无法同步
- Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
- Little red book timestamp2 (2022 / 04 / 22)
- 脏读、不可重复读和幻读介绍
- A series of problems about the best time to buy and sell stocks
猜你喜欢
Brute force of DVWA low -- > High
API gateway / API gateway (II) - use of Kong - load balancing
8.3 language model and data set
setcontext getcontext makecontext swapcontext
每日一题-LeetCode396-旋转函数-递推
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
How to design a good API interface?
Lotus DB design and Implementation - 1 Basic Concepts
nuxt项目:全局获取process.env信息
X509 certificate cer format to PEM format
随机推荐
The difference between having and where in SQL
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Thinkphp5 + data large screen display effect
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
SQLSERVER事物与锁的问题
Have you learned the basic operation of circular queue?
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
My raspberry PI zero 2W toss notes to record some problems and solutions
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
Application of skiplist in leveldb
脏读、不可重复读和幻读介绍
How to write the keywords in the cover and title? As we media, why is there no video playback
Share 3 tools, edit 5 works at home and earn more than 400
Flink datastream type system typeinformation
Three uses of kprobe
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
thinkphp5+数据大屏展示效果
About UDP receiving ICMP port unreachable