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 package melody
import ( import (
"github.com/gorilla/websocket"
"net/http" "net/http"
"sync" "sync"
"github.com/gorilla/websocket"
) )
type handleMessageFunc func(*Session, []byte) type handleMessageFunc func(*Session, []byte)
@ -17,6 +18,7 @@ type Melody struct {
Upgrader *websocket.Upgrader Upgrader *websocket.Upgrader
messageHandler handleMessageFunc messageHandler handleMessageFunc
messageHandlerBinary handleMessageFunc messageHandlerBinary handleMessageFunc
messageSentHandler handleMessageFunc
errorHandler handleErrorFunc errorHandler handleErrorFunc
connectHandler handleSessionFunc connectHandler handleSessionFunc
disconnectHandler handleSessionFunc disconnectHandler handleSessionFunc
@ -40,6 +42,7 @@ func New() *Melody {
Upgrader: upgrader, Upgrader: upgrader,
messageHandler: func(*Session, []byte) {}, messageHandler: func(*Session, []byte) {},
messageHandlerBinary: func(*Session, []byte) {}, messageHandlerBinary: func(*Session, []byte) {},
messageSentHandler: func(*Session, []byte) {},
errorHandler: func(*Session, error) {}, errorHandler: func(*Session, error) {},
connectHandler: func(*Session) {}, connectHandler: func(*Session) {},
disconnectHandler: func(*Session) {}, disconnectHandler: func(*Session) {},
@ -73,6 +76,11 @@ func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) {
m.messageHandlerBinary = fn 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. // HandleError fires fn when a session has an error.
func (m *Melody) HandleError(fn func(*Session, error)) { func (m *Melody) HandleError(fn func(*Session, error)) {
m.errorHandler = fn m.errorHandler = fn

4
session.go

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

Loading…
Cancel
Save