Skip to content

Commit

Permalink
feat: 添加发送文本消息
Browse files Browse the repository at this point in the history
  • Loading branch information
danmuking committed Apr 9, 2024
1 parent f3403d5 commit 807dca0
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 24 deletions.
3 changes: 2 additions & 1 deletion domain/enum/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package enum
import "github.com/gorilla/websocket"

const (
NEW_MESSAGE = websocket.TextMessage
NewMessage = websocket.TextMessage
TextMessage = 1
)
6 changes: 1 addition & 5 deletions event/listener/friend_apply_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"DiTing-Go/dal/model"
"DiTing-Go/domain/enum"
"DiTing-Go/global"
global2 "DiTing-Go/websocket/global"
"DiTing-Go/websocket/service"
"log"
)
Expand All @@ -17,9 +16,6 @@ func init() {

// FriendApplyEvent 好友申请事件
func FriendApplyEvent(apply model.UserApply) {
msg := global2.Msg{
Uid: apply.TargetID,
}
// 发送新消息事件
service.Send(&msg)
service.Send(apply.TargetID)
}
14 changes: 3 additions & 11 deletions event/listener/friend_new_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"DiTing-Go/domain/enum"
"DiTing-Go/global"
"DiTing-Go/pkg/utils"
global2 "DiTing-Go/websocket/global"
"context"
"log"
"sort"
Expand All @@ -23,11 +22,8 @@ func init() {
func FriendNewEvent(friend model.UserFriend) {
ctx := context.Background()
q := global.Query
msg := global2.Msg{
Uid: friend.FriendUID,
}
room := model.Room{
Type: enum.GROUP,
Type: enum.PERSONAL,
HotFlag: enum.NORMAL,
ExtJSON: "{}",
}
Expand Down Expand Up @@ -59,19 +55,15 @@ func FriendNewEvent(friend model.UserFriend) {
log.Fatalln("创建房间失败", err.Error())
return
}
//TODO 抽取为方法
newMsg := model.Message{
RoomID: room.ID,
FromUID: friend.UID,
Content: "你们已经是好友了,开始聊天吧",
// TODO: 抽取为常量
Status: 0,
Type: 1,
Type: enum.TextMessage,
Extra: "{}",
}
msgQ := q.WithContext(ctx).Message
msgQ.Create(&newMsg)

// 发送新消息事件
global.Bus.Publish("NewMsgEvent", msg)
global.Bus.Publish("NewMsgEvent", newMsg)
}
30 changes: 27 additions & 3 deletions event/listener/new_msg_event.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package listener

import (
"DiTing-Go/dal/model"
query "DiTing-Go/dal/query"
"DiTing-Go/domain/enum"
"DiTing-Go/global"
global2 "DiTing-Go/websocket/global"
"DiTing-Go/websocket/service"
"context"
"log"
)

Expand All @@ -16,6 +18,28 @@ func init() {
}

// NewMsgEvent 新消息事件
func NewMsgEvent(msg global2.Msg) {
service.Send(&msg)
func NewMsgEvent(msg model.Message) {
//TODO:修改会话表
// 向房间中的所有用户发送消息,包括自己
roomQ := global.Query.WithContext(context.Background()).Room
room, _ := roomQ.Where(query.Room.ID.Eq(msg.RoomID)).First()
// 单聊
if room.Type == enum.PERSONAL {
roomFriendQ := global.Query.WithContext(context.Background()).RoomFriend
roomFriend, _ := roomFriendQ.Where(query.RoomFriend.RoomID.Eq(room.ID)).First()
// 发送新消息事件
service.Send(roomFriend.Uid1)
service.Send(roomFriend.Uid2)
} else if room.Type == enum.GROUP {
roomGroupQ := global.Query.WithContext(context.Background()).RoomGroup
roomGroup, _ := roomGroupQ.Where(query.RoomGroup.RoomID.Eq(room.ID)).First()
// 查询所有群成员
groupMemberQ := global.Query.WithContext(context.Background()).GroupMember
groupMembers, _ := groupMemberQ.Where(query.GroupMember.GroupID.Eq(roomGroup.ID)).Find()
// 发送新消息事件
for _, groupMember := range groupMembers {
service.Send(groupMember.UID)
}
}

}
8 changes: 7 additions & 1 deletion routes/init_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func initGin() {
apiContact.GET("/test", test)
}

apiMsg := router.Group("/api/msg")
apiMsg.Use(jwt.JWT())
{
apiMsg.POST("textMsg", service.SendTextMsgService)
}

err := router.Run(":5000")
if err != nil {
return
Expand All @@ -72,6 +78,6 @@ func initGin() {
func test(c *gin.Context) {
msg := new(global.Msg)
msg.Uid = 20017
websocketService.Send(msg)
websocketService.Send(msg.Uid)
resp.SuccessResponse(c, nil)
}
63 changes: 63 additions & 0 deletions service/message_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package service

import (
"DiTing-Go/dal/model"
"DiTing-Go/domain/enum"
"DiTing-Go/global"
"DiTing-Go/pkg/resp"
"bytes"
"context"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
"io"
"log"
)

// SendTextMsgService 发送文本消息
func SendTextMsgService(c *gin.Context) {
uid := c.GetInt64("uid")
msg := model.Message{}
data, err := c.GetRawData()
if err != nil { //ShouldBind()会自动推导
resp.ErrorResponse(c, "参数错误")
c.Abort()
return
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(data))
if err := json.Unmarshal(data, &msg); err != nil {
resp.ErrorResponse(c, "参数错误")
c.Abort()
log.Fatalln("参数错误", err.Error())
return
}
msg.Type = enum.TextMessage
msg.FromUID = uid
if msg.Extra == "" {
msg.Extra = "{}"
}

// 发送消息
if err := SendTextMsg(msg); err != nil {
resp.ErrorResponse(c, "消息发送失败")
c.Abort()
return
}

// 发送新消息事件
global.Bus.Publish(enum.NewMessageEvent, msg)

// 返回成功
resp.SuccessResponseWithMsg(c, "success")
c.Abort()
return
}

func SendTextMsg(msg model.Message) error {
ctx := context.Background()
msgQ := global.Query.WithContext(ctx).Message
if err := msgQ.Create(&msg); err != nil {
log.Fatalln("消息发送失败", err.Error())
return err
}
return nil
}
5 changes: 2 additions & 3 deletions websocket/service/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func Connect(w http.ResponseWriter, r *http.Request) {
}

// Send 发送空消息代表有新消息,前端收到消息后再去后端拉取消息
func Send(msg *global.Msg) {
uid := msg.Uid
func Send(uid int64) {
stringUid := strconv.FormatInt(uid, 10)
channels, _ := global.UserChannelMap.Get(stringUid)
// 用户不在线,直接返回
Expand All @@ -92,7 +91,7 @@ func Send(msg *global.Msg) {
}
for _, conn := range channels.ChannelList {
// 发送空消息,代表有新消息
err := conn.WriteMessage(enum.NEW_MESSAGE, []byte("111"))
err := conn.WriteMessage(enum.NewMessage, []byte("111"))
if err != nil {
fmt.Println("写入错误")
break
Expand Down

0 comments on commit 807dca0

Please sign in to comment.