-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from gibsn/bug/issue_35_chat_refactoring
chat: major refactoring
- Loading branch information
Showing
9 changed files
with
361 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/technochat/chat/wsmessages.go → src/technochat/chat/message/message.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package chat | ||
package message | ||
|
||
type TypeID int | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,67 @@ | ||
package chat | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
const ( | ||
userSendBufferSize = 10 | ||
"technochat/chat/user" | ||
) | ||
|
||
type User struct { | ||
WS *websocket.Conn | ||
Name string | ||
ID int | ||
send chan WSMessage | ||
terminateSend chan struct{} | ||
func (c *Chat) AddUser(ws *websocket.Conn) *user.User { | ||
if c.restJoins <= 0 { | ||
return nil | ||
} | ||
|
||
usr := user.NewUser(ws) | ||
usr.Name, usr.ID = c.ChatNames.GenerateNameID() | ||
|
||
log.Printf("info: chat: new user [%d %s] in chat %s", usr.ID, usr.Name, c.ID) | ||
|
||
c.correspsMx.Lock() | ||
c.corresps[usr.ID] = usr | ||
c.correspsMx.Unlock() | ||
c.restJoins-- | ||
|
||
c.usersWG.Add(1) | ||
c.userConnectedChan <- usr | ||
|
||
return usr | ||
} | ||
|
||
func NewUser() *User { | ||
return &User{ | ||
send: make(chan WSMessage, userSendBufferSize), | ||
func (c *Chat) DelUser(id int) { | ||
c.correspsMx.Lock() | ||
|
||
usr, ok := c.corresps[id] | ||
if !ok { | ||
c.correspsMx.Unlock() | ||
return | ||
} | ||
|
||
log.Printf("info: chat: deleting user [%d, %s] in chat %s", usr.ID, usr.Name, c.ID) | ||
|
||
delete(c.corresps, id) | ||
c.correspsMx.Unlock() | ||
|
||
c.userDisconnectedChan <- usr | ||
c.usersWG.Done() | ||
} | ||
|
||
func (u *User) SendEvent(event EventID, i interface{}) { | ||
u.WS.WriteJSON(WSMessage{ | ||
Type: WSMsgTypeService, | ||
Data: Event{event, i}, | ||
}) | ||
func (c *Chat) SubscribeUser(usr *user.User) { | ||
go func() { | ||
for msg := range usr.ReadStream() { | ||
msg.Name = usr.Name | ||
c.incomingChan <- msg | ||
} | ||
}() | ||
} | ||
|
||
func SendEvent(ws *websocket.Conn, event EventID, i interface{}) { | ||
ws.WriteJSON(WSMessage{ | ||
Type: WSMsgTypeService, | ||
Data: Event{event, i}, | ||
}) | ||
func (c *Chat) ShutdownUsers() { | ||
c.correspsMx.Lock() | ||
for _, usr := range c.corresps { | ||
usr.TriggerShutdown() | ||
} | ||
c.correspsMx.Unlock() | ||
|
||
c.usersWG.Wait() | ||
} |
Oops, something went wrong.