当前位置:网站首页>微信小程序客服接入,实现发送和接收消息
微信小程序客服接入,实现发送和接收消息
2022-04-23 14:08:00 【阿闰】
公司多有多个小程序客服功能,实现统一接入聊天
每个小程序通知接入的通知地址不一样,但接入流程逻辑一样,在接入时标识所属小程序
netty统一发送消息到页面websocket部分,要定义好自己业务需要的消息体
@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("调用netty发送消息信息:" + jsonMsg);
try {
// 给指定用户发
// ChannelUser channelUser = ChannelConst.getChannelUser("1512312");
// if(channelUser==null) {
// lg.info("微信通知调用netty发送消息到userId:" + null);
// return;
// }
// 给所有在线用户发
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集成部分,
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private static final Logger logger = LoggerFactory.getLogger(ChatHandler.class);
/**
* 广播获取消息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (null != msg && msg instanceof FullHttpRequest) {
// 第一次必进这里,认为这里链接成功
FullHttpRequest request = (FullHttpRequest) msg;
String uri = request.uri();
Map paramMap = getUrlParams(uri);
// 身份识别和sysuser做处理
System.out.println("channelRead接收连接的的参数是:" + 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("身份识别成功,客户端channelMap数量:" + ChannelConst.clients.size());
}else {
ctx.close();//不让他连接
System.err.println("去掉不正常的连接===========================================");
return ;
}
String newUri = uri.substring(0, uri.indexOf("?"));
request.setUri(newUri);
}
if(msg instanceof TextWebSocketFrame) {
TextWebSocketFrame frame = (TextWebSocketFrame) msg;
System.out.println("接收到客户端的消息:" + frame.text());
// 将接收到的消息发送到所有的客户端
for (Channel channel : ChannelConst.clients) {
// 注意所有的websocket数据都应该以TextWebSocketFrame进行封装
channel.writeAndFlush(
new TextWebSocketFrame("服务器接收的消息:" + LocalDateTime.now() + ",消息是:" + frame.text()));
}
}
// if(msg instanceof CloseWebSocketFrame) {
// logger.info("有退出的用户===========================================");
// }else {
// logger.info("有其他消息===========================================");
// }
super.channelRead(ctx, msg);
}
高仿微信的界面,可以发图片或文字,不同的客户由不同的客服接入聊天
版权声明
本文为[阿闰]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wasd986523/article/details/118633046
边栏推荐
猜你喜欢
星界边境Starbound创意工坊订阅的mod的存放路径
MySQL数据库讲解(十)
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
修改ddt生成的测试用例名称
使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
MySQL数据库讲解(七)
更改plsql工具栏的图标大小
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
多云数据流转?云上容灾?年前最后的价值内容分享
随机推荐
报表FCRA考试题集及答案(错了11题)
帆软报表设置单元格填报以及根据值的大小进行排名方法
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
MySQL数据库讲解(九)
Three point positioning based on ibeacons (wechat applet)
mysql 5.1升级到5.611
MySQL数据库讲解(七)
mysql 5.1升级到5.68
预览CSV文件
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍
封装logging模块
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
redis数据库讲解二(redis高可用、持久化、性能管理)
返回数组排序后下标
leetcode--380. O (1) time insertion, deletion and acquisition of random elements
leetcode--977. Squares of a Sorted Array
POI operation word template replaces data and exports word