Browse Source

split HandleSentMessage into text and binary methods

master
Ola Holmström 8 years ago
parent
commit
aea0b4aedf
  1. 1
      CHANGELOG.md
  2. 31
      melody.go
  3. 7
      session.go

1
CHANGELOG.md

@ -2,6 +2,7 @@
* Return errors for some exposed methods.
* Add `HandleRequestWithKeys`.
* Add `HandleSentMessage` and `HandleSentMessageBinary`.
## 2017-01-20

31
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

7
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()
}

Loading…
Cancel
Save