diff --git a/CHANGELOG.md b/CHANGELOG.md index 0441908..7ebb5f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Return errors for some exposed methods. * Add `HandleRequestWithKeys`. +* Add `HandleSentMessage` and `HandleSentMessageBinary`. ## 2017-01-20 diff --git a/melody.go b/melody.go index 1554acc..331020f 100644 --- a/melody.go +++ b/melody.go @@ -14,15 +14,17 @@ type filterFunc func(*Session) bool // Melody implements a websocket manager. type Melody struct { - Config *Config - Upgrader *websocket.Upgrader - messageHandler handleMessageFunc - messageHandlerBinary handleMessageFunc - errorHandler handleErrorFunc - connectHandler handleSessionFunc - disconnectHandler handleSessionFunc - pongHandler handleSessionFunc - hub *hub + Config *Config + Upgrader *websocket.Upgrader + messageHandler handleMessageFunc + messageHandlerBinary handleMessageFunc + messageSentHandler handleMessageFunc + messageSentHandlerBinary handleMessageFunc + errorHandler handleErrorFunc + connectHandler handleSessionFunc + disconnectHandler handleSessionFunc + pongHandler handleSessionFunc + hub *hub } // New creates a new melody instance with default Upgrader and Config. @@ -41,6 +43,7 @@ func New() *Melody { Upgrader: upgrader, messageHandler: func(*Session, []byte) {}, messageHandlerBinary: func(*Session, []byte) {}, + messageSentHandler: func(*Session, []byte) {}, errorHandler: func(*Session, error) {}, connectHandler: func(*Session) {}, disconnectHandler: func(*Session) {}, @@ -74,6 +77,16 @@ func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) { m.messageHandlerBinary = fn } +// HandleSentMessage fires fn when a text message is successfully sent. +func (m *Melody) HandleSentMessage(fn func(*Session, []byte)) { + m.messageSentHandler = fn +} + +// HandleSentMessageBinary fires fn when a binary message is successfully sent. +func (m *Melody) HandleSentMessageBinary(fn func(*Session, []byte)) { + m.messageSentHandler = fn +} + // HandleError fires fn when a session has an error. func (m *Melody) HandleError(fn func(*Session, error)) { m.errorHandler = fn diff --git a/session.go b/session.go index 6b60857..00aab67 100644 --- a/session.go +++ b/session.go @@ -91,6 +91,13 @@ loop: break loop } + if msg.t == websocket.TextMessage { + s.melody.messageSentHandler(s, msg.msg) + } + + if msg.t == websocket.BinaryMessage { + s.melody.messageSentHandlerBinary(s, msg.msg) + } case <-ticker.C: s.ping() }