Skip to content

Commit

Permalink
also send username in msg
Browse files Browse the repository at this point in the history
  • Loading branch information
ruskaof committed Mar 25, 2024
1 parent 154bdf5 commit 38d78cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type WebSocketClientMessage struct {
}

type WebSocketServerMessage struct {
ChatId int64 `json:"chatId"`
UserId int64 `json:"userId"`
Message string `json:"message"`
ChatId int64 `json:"chatId"`
UserId int64 `json:"userId"`
Username string `json:"username"`
Message string `json:"message"`
}

func SerializeWebSocketServerMessage(message WebSocketServerMessage) []byte {
Expand Down
9 changes: 6 additions & 3 deletions src/service/usrserv/usrserv.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ func (us *UserService) handleMessage(message chat.IncomingClientMessage) {

// check if user is in chat
inChat := false
var username string
var receiversUserIds []int64
for _, user := range chatEntity.Users {
if user.ID == message.ClientId {
inChat = true
username = user.Username
}
receiversUserIds = append(receiversUserIds, user.ID)
}
Expand All @@ -175,9 +177,10 @@ func (us *UserService) handleMessage(message chat.IncomingClientMessage) {
}

serverMessage := dto.WebSocketServerMessage{
ChatId: message.Message.ChatId,
UserId: message.ClientId,
Message: message.Message.Message,
ChatId: message.Message.ChatId,
UserId: message.ClientId,
Username: username,
Message: message.Message.Message,
}

us.wsHub.SendToSelected(serverMessage, receiversUserIds)
Expand Down

0 comments on commit 38d78cc

Please sign in to comment.