Skip to content

Commit

Permalink
feat(im):优化im体验,添加错误处理以及json消息解析
Browse files Browse the repository at this point in the history
  • Loading branch information
li1553770945 committed Apr 23, 2023
1 parent 902991c commit a26eba1
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 3 deletions.
4 changes: 4 additions & 0 deletions biz/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

import (
"personal-page-be/biz/infra/config"
"personal-page-be/biz/internal/service/chat"
"personal-page-be/biz/internal/service/file"
"personal-page-be/biz/internal/service/global_service"
"personal-page-be/biz/internal/service/message"
Expand All @@ -14,19 +15,22 @@ type Container struct {
FileService file.IFileService
GlobalService global_service.IGlobalService
MessageService message.IMessageService
ChatService chat.IChatService
}

func NewContainer(config *config.Config, userService user.IUserService,
fileService file.IFileService,
globalService global_service.IGlobalService,
messageService message.IMessageService,
chatService chat.IChatService,
) *Container {
return &Container{
Config: config,
UserService: userService,
FileService: fileService,
GlobalService: globalService,
MessageService: messageService,
ChatService: chatService,
}

}
6 changes: 6 additions & 0 deletions biz/container/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package container

import (
"github.com/google/wire"
"personal-page-be/biz/infra/cache"
"personal-page-be/biz/infra/config"
"personal-page-be/biz/infra/database"
"personal-page-be/biz/infra/log"
"personal-page-be/biz/internal/repo"
"personal-page-be/biz/internal/service/chat"
"personal-page-be/biz/internal/service/file"
"personal-page-be/biz/internal/service/global_service"
"personal-page-be/biz/internal/service/message"
Expand All @@ -20,6 +23,8 @@ func GetContainer(path string) *Container {
//infra
config.InitConfig,
database.NewDatabase,
cache.NewCache,
log.NewLogger,

//repo
repo.NewRepository,
Expand All @@ -29,6 +34,7 @@ func GetContainer(path string) *Container {
file.NewFileService,
global_service.NewGlobalService,
message.NewMessageService,
chat.NewChatService,

NewContainer,
))
Expand Down
8 changes: 7 additions & 1 deletion biz/container/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions biz/infra/cache/go-cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cache

import (
"github.com/patrickmn/go-cache"
"time"
)

func NewCache() *cache.Cache {
c := cache.New(1*time.Hour, 2*time.Hour)
return c
}
33 changes: 31 additions & 2 deletions biz/infra/log/log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package log

import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"runtime"
)

func getLogLevel() logrus.Level {
// 设置日志级别,可以根据需要修改
return logrus.DebugLevel
}

func getLogFormatter() logrus.Formatter {
// 定义日志输出格式
return &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05", //时间格式,
CallerPrettyfier: callerPrettyfier,
}
}

func callerPrettyfier(f *runtime.Frame) (string, string) {
// 格式化输出文件名和行号
filename := filepath.Base(f.File)
return fmt.Sprintf("%s:%d", filename, f.Line), ""
}

func NewLogger() *logrus.Logger {
log := logrus.New()
return log
// 初始化日志
logger := logrus.New()
logger.SetLevel(getLogLevel())
logger.SetOutput(os.Stdout)
logger.SetFormatter(getLogFormatter())

return logger
}
31 changes: 31 additions & 0 deletions biz/internal/domain/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain

import (
"github.com/hertz-contrib/websocket"
"golang.org/x/sync/semaphore"
"sync"
"time"
)

type ChatMessageEntity struct {
Type string `json:"type"`
Data string `json:"data"`
Time time.Time `json:"time"`
ErrorMsg string `json:"error_msg"`
}

type ChatEntity struct {
ChatID string
CreaterID string
JoinerID string
CreaterMessageLock sync.Mutex
JoinerMessageLock sync.Mutex
CreaterMessageWriteSem *semaphore.Weighted
CreaterMessageReadSem *semaphore.Weighted
JoinerMessageWriteSem *semaphore.Weighted
JoinerMessageReadSem *semaphore.Weighted
CreaterConn *websocket.Conn
JoinerConn *websocket.Conn
MessageSendByCreater []*ChatMessageEntity
MessageSendByJoiner []*ChatMessageEntity
}
11 changes: 11 additions & 0 deletions biz/internal/dto/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dto

type SendMessageReq struct {
ClientID string `json:"client_id"`
Message string `json:"message"`
ChatID string `json:"chat_id"`
}

type JoinChatReq struct {
ChatID string `json:"chat_id"`
}
Loading

0 comments on commit a26eba1

Please sign in to comment.