Skip to content

Commit

Permalink
Functionality is pretty complete now. (#4)
Browse files Browse the repository at this point in the history
- in session.go even though the server tells us the amount of connections when connecting, there is no way to keep track of this number throughout the lifetime of our connection.
- added new handlers for missing functions, including handling of websocket errors in "userspace".
- Reconnection logic is pretty complicated, I tried different approaches but still not happy. Works well enough for now...
  • Loading branch information
xDashh authored and Kirill Voloshin committed Dec 9, 2017
1 parent dcebcb9 commit 97642b4
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 91 deletions.
2 changes: 1 addition & 1 deletion dggchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New(args ...string) (*Session, error) {
}

s := &Session{
AttempToReconnect: true,
attempToReconnect: true,
state: newState(),
wsURL: wsURL,
dialer: websocket.DefaultDialer,
Expand Down
13 changes: 13 additions & 0 deletions eventHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dggchat

type handlers struct {
msgHandler func(Message, *Session)
namesHandler func(Names, *Session)
muteHandler func(Mute, *Session)
unmuteHandler func(Mute, *Session)
banHandler func(Ban, *Session)
Expand All @@ -13,13 +14,20 @@ type handlers struct {
broadcastHandler func(Broadcast, *Session)
pingHandler func(Ping, *Session)
subOnlyHandler func(SubOnly, *Session)

socketErrorHandler func(error, *Session)
}

// AddMessageHandler adds a function that will be called every time a message is received
func (s *Session) AddMessageHandler(fn func(Message, *Session)) {
s.handlers.msgHandler = fn
}

// AddNamesHandler adds a function that will be called every time a names message is received
func (s *Session) AddNamesHandler(fn func(Names, *Session)) {
s.handlers.namesHandler = fn
}

// AddMuteHandler adds a function that will be called every time a mute message is received
func (s *Session) AddMuteHandler(fn func(Mute, *Session)) {
s.handlers.muteHandler = fn
Expand Down Expand Up @@ -74,3 +82,8 @@ func (s *Session) AddPingHandler(fn func(Ping, *Session)) {
func (s *Session) AddSubOnlyHandler(fn func(SubOnly, *Session)) {
s.handlers.subOnlyHandler = fn
}

// AddSocketErrorHandler adds a function that will be called every time a socket error occurs
func (s *Session) AddSocketErrorHandler(fn func(error, *Session)) {
s.handlers.socketErrorHandler = fn
}
12 changes: 5 additions & 7 deletions messageStructs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
FeatureBot2 = "flair11"
FeatureBroadcaster = "flair12"
FeatureTier1 = "flair13"
FeatureBirthday = "flair15"
)

// Constants for different types of errors the chat can return
Expand Down Expand Up @@ -76,7 +77,8 @@ type (
Online bool
}

namesMessage struct {
// Names reprents the initial status message containing user information
Names struct {
Connections int `json:"connectioncount"`
Users []User `json:"users"`
}
Expand Down Expand Up @@ -116,13 +118,9 @@ type (

// Broadcast represents a chat broadcast
Broadcast struct {
Message string
Sender User
Timestamp time.Time
}

broadcast struct {
Data string `json:"data"`
Timestamp int64 `json:"timestamp"`
Message string
}

// Ping represents a pong response from the server
Expand Down
24 changes: 15 additions & 9 deletions parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func parseBan(s string, sess *Session) (Ban, error) {
return ban, nil
}

func parseNames(s string) (namesMessage, error) {
var nm namesMessage
err := json.Unmarshal([]byte(s), &nm)
func parseNames(s string) (Names, error) {
var n Names
err := json.Unmarshal([]byte(s), &n)
if err != nil {
return namesMessage{}, err
return Names{}, err
}

return nm, nil
return n, nil
}

func parseRoomAction(s string) (RoomAction, error) {
Expand Down Expand Up @@ -134,16 +134,22 @@ func parsePrivateMessage(s string, sess *Session) (PrivateMessage, error) {
}

func parseBroadcast(s string) (Broadcast, error) {
var b broadcast
var m message

err := json.Unmarshal([]byte(s), &b)
err := json.Unmarshal([]byte(s), &m)
if err != nil {
return Broadcast{}, err
}

user := User{
Nick: m.Nick,
Features: m.Features,
}

broadcast := Broadcast{
Message: b.Data,
Timestamp: unixToTime(b.Timestamp),
Sender: user,
Message: m.Data,
Timestamp: unixToTime(m.Timestamp),
}

return broadcast, nil
Expand Down
Loading

0 comments on commit 97642b4

Please sign in to comment.