Browse Source

Add a handler for message successfully sent

master
Robbie Trencheny 8 years ago
parent
commit
7ba6d478f0
  1. 10
      melody.go
  2. 4
      session.go

10
melody.go

@ -1,9 +1,10 @@
package melody
import (
"github.com/gorilla/websocket"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
type handleMessageFunc func(*Session, []byte)
@ -17,6 +18,7 @@ type Melody struct {
Upgrader *websocket.Upgrader
messageHandler handleMessageFunc
messageHandlerBinary handleMessageFunc
messageSentHandler handleMessageFunc
errorHandler handleErrorFunc
connectHandler handleSessionFunc
disconnectHandler handleSessionFunc
@ -40,6 +42,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) {},
@ -73,6 +76,11 @@ func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) {
m.messageHandlerBinary = fn
}
// HandleSentMessage fires fn when a message is successfully sent.
func (m *Melody) HandleSentMessage(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

4
session.go

@ -2,10 +2,11 @@ package melody
import (
"errors"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
// Session is wrapper around websocket connections.
@ -71,6 +72,7 @@ loop:
s.melody.errorHandler(s, err)
break loop
}
s.melody.messageSentHandler(s, msg.msg)
case <-ticker.C:
s.ping()
}

Loading…
Cancel
Save