Skip to content

Commit

Permalink
fix leave room error
Browse files Browse the repository at this point in the history
  • Loading branch information
szpnygo committed Oct 13, 2022
1 parent c5abb1e commit 4dc4cfd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gtc is a p2p terminal chat app, using webrtc. All conversation content will not
### Install

```bash
go install github.com/szpnygo/gtc@v0.2.2
go install github.com/szpnygo/gtc@v0.2.3
```

or you can download from release
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/gorilla/websocket v1.5.0
github.com/mattn/go-runewidth v0.0.14
github.com/metagogs/gogs v0.1.4
github.com/metagogs/gogs v0.1.5
github.com/nsf/termbox-go v1.1.1
github.com/pion/webrtc/v3 v3.1.47
github.com/pterm/pterm v0.12.49
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/metagogs/gogs v0.1.4 h1:Mqbk/rzabfjJ2UG9dgm1buLEpAe4OwBVtv3HV8wY+gE=
github.com/metagogs/gogs v0.1.4/go.mod h1:yUr+u6FRemVcHv5xhvGL868f+mYIdG7R2dta57atyJo=
github.com/metagogs/gogs v0.1.5 h1:rN+HKF5IX3F1CpcTjYRmT7Ux79ZFk1Yi21AkwvvFIJY=
github.com/metagogs/gogs v0.1.5/go.mod h1:yUr+u6FRemVcHv5xhvGL868f+mYIdG7R2dta57atyJo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
61 changes: 37 additions & 24 deletions server/internal/logic/gtcserver/join_room_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/metagogs/gogs/group"
"github.com/metagogs/gogs/gslog"
"github.com/metagogs/gogs/session"
"github.com/szpnygo/gtc/server/internal/message"
Expand Down Expand Up @@ -35,41 +36,53 @@ func (l *JoinRoomLogic) Handler(in *model.JoinRoom) {
uid := fmt.Sprintf("%d_%s", l.session.ID(), in.Name)
l.session.SetUID(uid)

// when the session is closed, remove the user from the room
l.session.SetOnCloseCallback(func(id int64) {
for _, name := range l.svcCtx.GroupList {
if g, ok := l.svcCtx.GS.GetGroup(name); ok {
l.removeUserFromGroup(g, in.Name)
}
}
})

group, exist := l.svcCtx.GS.GetGroup(in.RoomId)
if !exist {
// the room does not exist
return
}

// join room
if err := group.AddUser(l.ctx, l.session.UID()); err == nil {
users := group.GetUsers(l.ctx)
usersFilted := filterUid(users)
if err := group.AddUser(l.ctx, l.session.UID()); err != nil {
return
}

_ = message.SendJoinRoomSuccess(l.session, &model.JoinRoomSuccess{
RoomId: group.GetGroupName(l.ctx),
UserId: l.session.ID(),
Users: usersFilted,
})
users := group.GetUsers(l.ctx)
usersFilted := filterUid(users)

session.BroadcastMessage(users, &model.JoinRoomNotify{
RoomId: group.GetGroupName(l.ctx),
Name: in.Name,
UserId: l.session.ID(),
Users: usersFilted,
}, nil, l.session.UID())
_ = message.SendJoinRoomSuccess(l.session, &model.JoinRoomSuccess{
RoomId: group.GetGroupName(l.ctx),
UserId: l.session.ID(),
Users: usersFilted,
})

// when the session is closed, remove the user from the room
l.session.OnClose(func(id int64) {
_ = group.RemoveUser(l.ctx, l.session.UID())
session.BroadcastMessage(users, &model.LeaveRoomNotify{
RoomId: group.GetGroupName(l.ctx),
Name: in.Name,
Users: filterUid(group.GetUsers(l.ctx)),
}, nil)
})
}
session.BroadcastMessage(users, &model.JoinRoomNotify{
RoomId: group.GetGroupName(l.ctx),
Name: in.Name,
UserId: l.session.ID(),
Users: usersFilted,
}, nil, l.session.UID())
}

func (l *JoinRoomLogic) removeUserFromGroup(g group.Group, name string) {
if err := g.RemoveUser(l.ctx, l.session.UID()); err == nil {
// broadcast the user left message if user is in the room
list := g.GetUsers(l.ctx)
session.BroadcastMessage(list, &model.LeaveRoomNotify{
RoomId: g.GetGroupName(l.ctx),
Name: name,
Users: filterUid(list),
}, nil)
}
}

func filterUid(uids []string) []*model.User {
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.2
v0.2.3

0 comments on commit 4dc4cfd

Please sign in to comment.