# go_chat **Repository Path**: hs9264/go_chat ## Basic Information - **Project Name**: go_chat - **Description**: websocket+mongodb 聊天 - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-12-21 - **Last Updated**: 2022-12-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 架构 ![](docs/架构.png) websocket+mongodb mongodb操作文档 ## 单聊 A给B发送Msg: A界面输入Msg,B界面显示Msg,同时A界面响应消息发送状态(对方是否接收) 每一个客户端需要进行Read和Write: - Read , 循环,从界面读消息,根据消息类型进行相应处理,比如单聊是读消息后发送消息, 具体如何发送?通过broadcast通道进行中转,到对方的接收buffer中,每个客户端都有一个接收buffer通道用来接收消息 - Write , 循环监听buffer通道,将消息写到http界面中去 socketconn.writeMessage ![](docs/发送消息流程.jpg) #### BUG: fail to Find:cannot transform type bson.D to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel 将mongodb.go 导入的包"gopkg.in/mgo.v2/bson" 替换为"go.mongodb.org/mongo-driver/bson" ### API格式 - type,定义消息类型; type=1 单聊、 type=2 查找所有历史消息,根据双方聊天记录进行排序且限制数量(最近历史消息)、 type=3 查找某用户发送的未读消息,排序 - content, 消息内容 - send_to_id, 指定接收方id #### 用户2向用户1发送单聊消息 ```json ws://localhost:3000/ws?uid=2 { "type":1, "content": "hello, sir 1 未读~", "send_to_id":"2" } ``` ![](docs/websocket1.jpg) #### 用户1查找用户2发送的未读消息 ```json ws://localhost:3000/ws?uid=1 { "type":3, "content": "", "send_to_id":"2" } ``` ![](docs/websocket3.jpg) ## docker运行 文档: https://docs.docker.com/build/hellobuild/ - 构建 根据当前目录下的dockerfile构建镜像,镜像仓库名为go_chat_demo ```shell docker build -t go_chat_demo:latest . ``` - 运行 ```shell docker run -p 3000:3000 go_chat_demo:latest --name go_chat ``` docker-compose.yml部署方式参考文档,未验证