AI prompts
base on
#### 项目介绍
1. 聊天盒子是一个仿微信实现的网页版聊天软件,目前完全开源,仅用于学习和交流。
2. 支持私聊、群聊、离线消息、发送图片、文件、好友在线状态显示等功能。
3. 后端采用springboot+netty实现,前端使用vue。
4. 服务器支持集群化部署,每个chat-server仅处理自身连接用户的消息
#### 项目结构
| 模块 | 功能 |
|---------------|------------|
| chat-platform | 与页面进行交互,处理业务请求 |
| chat-server | 推送聊天消息|
| chat-client | 消息推送sdk|
| chat-common | 公共包 |
#### 消息推送方案
![输入图片说明](img_1.png)
- 当消息的发送者和接收者连的不是同一个server时,消息是无法直接推送的,所以我们需要设计出能够支持跨节点推送的方案
- 利用了redis的list数据实现消息推送,其中key为im:unread:${serverid},每个key的数据可以看做一个queue,每个chat-server根据自身的id只消费属于自己的queue
- redis记录了每个用户的websocket连接的是哪个chat-server,当用户发送消息时,chat-platform将根据所连接的chat-server的id,决定将消息推向哪个queue
#### 本地快速部署
1.安装运行环境
- 安装node:v14.16.0
- 安装jdk:1.8
- 安装maven:3.6.3
- 安装mysql:5.7,密码分别为root/root,运行sql脚本(脚本在chat-platfrom的resources/db目录)
- 安装redis:4.0
- 安装minio,命令端口使用9001,并创建一个名为"chat-box"的bucket,并设置访问权限为公开
2.启动后端服务
```
mvn clean package
java -jar ./chat-platform/target/chat-platform.jar
java -jar ./chat-server/target/chat-server.jar
```
3.启动前端ui
```
cd chat-ui
npm install
npm run serve
```
4.访问localhost:8080
#### 快速接入
消息推送的请求代码已经封装在chat-client包中,对于需要接入chat-server的小伙伴,可以按照下面的教程快速的将聊天功能集成到自己的项目中。
注意服务器端和网页端都需要接入,服务器端发送消息,网页端接收消息。
4.1 服务器端接入
引入pom文件
```
<dependency>
<groupId>com.box</groupId>
<artifactId>chat-client</artifactId>
<version>1.1.0</version>
</dependency>
```
内容使用了redis进行通信,所以要配置redis地址:
```
spring:
redis:
host: 127.0.0.1
port: 6379
```
直接把IMClient通过@Autowire导进来就可以发送消息了,IMClient 只有2个接口:
```
public class IMClient {
/**
* 发送私聊消息
*
* @param recvId 接收用户id
* @param messageInfo 消息体,将转成json发送到客户端
*/
void sendPrivateMessage(Long recvId, PrivateMessageInfo... messageInfo);
/**
* 发送群聊消息
*
* @param recvIds 群聊用户id列表
* @param messageInfo 消息体,将转成json发送到客户端
*/
void sendGroupMessage(List<Long> recvIds, GroupMessageInfo... messageInfo);
}
```
发送私聊消息(群聊也是类似的方式):
```
@Autowired
private IMClient imClient;
public void sendMessage(){
PrivateMessageInfo messageInfo = new PrivateMessageInfo();
Long recvId = 1L;
messageInfo.setId(123L);
messageInfo.setContent("你好呀");
messageInfo.setType(MessageType.TEXT.getCode());
messageInfo.setSendId(userId);
messageInfo.setRecvId(recvId);
messageInfo.setSendTime(new Date());
imClient.sendPrivateMessage(recvId,messageInfo);
}
```
如果需要对消息发送的结果进行监听的话,实现MessageListener,并加上@IMListener即可
```
@Slf4j
@IMListener(type = IMListenerType.ALL)
public class PrivateMessageListener implements MessageListener {
@Override
public void process(SendResult result){
PrivateMessageInfo messageInfo = (PrivateMessageInfo) result.getMessageInfo();
if(result.getStatus().equals(IMSendStatus.SUCCESS)){
// 消息发送成功
log.info("消息已读,消息id:{},发送者:{},接收者:{}",messageInfo.getId(),messageInfo.getSendId(),messageInfo.getRecvId());
}
}
}
```
4.2 网页端接入
首先将chat-ui/src/api/wssocket.js拷贝到自己的项目。
接入代码如下:
```
import * as wsApi from './api/wssocket';
let wsUrl = 'ws://localhost:8878/im'
let userId = 1;
wsApi.createWebSocket(wsUrl , userId);
wsApi.onopen(() => {
// 连接打开
console.log("连接成功");
});
wsApi.onmessage((cmd,messageInfo) => {
if (cmd == 2) {
// 异地登录,强制下线
console.log("您已在其他地方登陆,将被强制下线");
} else if (cmd == 3) {
// 私聊消息
console.log(messageInfo);
} else if (cmd == 4) {
// 群聊消息
console.log(messageInfo);
}
})
```
#### 界面截图
文字聊天:
![输入图片说明](%E6%88%AA%E5%9B%BE/%E6%96%87%E5%AD%97%E8%81%8A%E5%A4%A9.jpg)
发送图片、文件:
![输入图片说明](%E6%88%AA%E5%9B%BE/%E5%8F%91%E9%80%81%E5%9B%BE%E7%89%87%E6%96%87%E4%BB%B6.jpg)
发送语音
![输入图片说明](%E6%88%AA%E5%9B%BE/%E5%8F%91%E9%80%81%E8%AF%AD%E9%9F%B3.jpg)
群聊:
![输入图片说明](%E6%88%AA%E5%9B%BE/%E7%BE%A4%E8%81%8A.jpg)
好友列表:
![输入图片说明](%E6%88%AA%E5%9B%BE/%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8.jpg)
群聊列表:
![输入图片说明](%E6%88%AA%E5%9B%BE/%E7%BE%A4%E8%81%8A%E5%88%97%E8%A1%A8.jpg)
", Assign "at most 3 tags" to the expected json: {"id":"214","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"