Skip to content

Commit

Permalink
Add a Destroy method to the room manager
Browse files Browse the repository at this point in the history
All connections in the room will leave the room (which then causes a cleanup)
  • Loading branch information
Jeff Mitchell committed Nov 5, 2013
1 parent 8929018 commit 4d95843
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
tags
16 changes: 16 additions & 0 deletions room_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type RoomManager struct {
leave chan *roomReq
// Channel of leave all requests, essentially cleaning up every trace of the specified connection.
leaveAll chan *Connection
// Channel of room destruction requests
destroy chan string
// Channel of messages associated with this room manager
send chan *roomMsg
// Stop signal channel
Expand All @@ -79,6 +81,7 @@ func NewRoomManager() *RoomManager {
join: make(chan *roomReq),
leave: make(chan *roomReq),
leaveAll: make(chan *Connection),
destroy: make(chan string),
send: make(chan *roomMsg, roomSendChannelSize),
stop: make(chan bool),
callbackRoomCreation: func(string) {},
Expand Down Expand Up @@ -143,6 +146,14 @@ func (rm *RoomManager) run() {
}
delete(rm.members, conn) // Remove map of joined lobbies
}
case name := <-rm.destroy:
if m, ok := rm.rooms[name]; ok {
// This should result inthe room being stopped/destroyed when the last
// connection is dropped
for conn := range m.room.members {
rm.leaveRoomByName(name, conn)
}
}
// Send
case rMsg := <-rm.send:
if m, ok := rm.rooms[rMsg.to]; ok { // If room exists, get it and send data to it.
Expand Down Expand Up @@ -199,6 +210,11 @@ func (rm *RoomManager) Stop() {
rm.stop <- true
}

// Remove connections from a particular room and delete the room
func (rm *RoomManager) Destroy(name string) {
rm.destroy <- name
}

// The room manager can emit several events. At the moment there are two events:
// "create" - triggered if a room was created and
// "remove" - triggered when a room was removed because of insufficient users
Expand Down

0 comments on commit 4d95843

Please sign in to comment.