|
|
@ -12,15 +12,16 @@ type filterFunc func(*Session) bool |
|
|
|
|
|
|
|
type Melody struct { |
|
|
|
Config *Config |
|
|
|
Upgrader *websocket.Upgrader |
|
|
|
MessageHandler handleMessageFunc |
|
|
|
ErrorHandler handleErrorFunc |
|
|
|
ConnectHandler handleSessionFunc |
|
|
|
DisconnectHandler handleSessionFunc |
|
|
|
upgrader *websocket.Upgrader |
|
|
|
messageHandler handleMessageFunc |
|
|
|
errorHandler handleErrorFunc |
|
|
|
connectHandler handleSessionFunc |
|
|
|
disconnectHandler handleSessionFunc |
|
|
|
hub *hub |
|
|
|
} |
|
|
|
|
|
|
|
func Default() *Melody { |
|
|
|
// Returns a new melody instance.
|
|
|
|
func New() *Melody { |
|
|
|
upgrader := &websocket.Upgrader{ |
|
|
|
ReadBufferSize: 1024, |
|
|
|
WriteBufferSize: 1024, |
|
|
@ -32,33 +33,38 @@ func Default() *Melody { |
|
|
|
|
|
|
|
return &Melody{ |
|
|
|
Config: newConfig(), |
|
|
|
Upgrader: upgrader, |
|
|
|
MessageHandler: func(*Session, []byte) {}, |
|
|
|
ErrorHandler: func(*Session, error) {}, |
|
|
|
ConnectHandler: func(*Session) {}, |
|
|
|
DisconnectHandler: func(*Session) {}, |
|
|
|
upgrader: upgrader, |
|
|
|
messageHandler: func(*Session, []byte) {}, |
|
|
|
errorHandler: func(*Session, error) {}, |
|
|
|
connectHandler: func(*Session) {}, |
|
|
|
disconnectHandler: func(*Session) {}, |
|
|
|
hub: hub, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (m *Melody) HandleConnect(fn handleSessionFunc) { |
|
|
|
m.ConnectHandler = fn |
|
|
|
// Fires fn when a session connects.
|
|
|
|
func (m *Melody) HandleConnect(fn func(*Session)) { |
|
|
|
m.connectHandler = fn |
|
|
|
} |
|
|
|
|
|
|
|
func (m *Melody) HandleDisconnect(fn handleSessionFunc) { |
|
|
|
m.DisconnectHandler = fn |
|
|
|
// Fires fn when a session disconnects.
|
|
|
|
func (m *Melody) HandleDisconnect(fn func(*Session)) { |
|
|
|
m.disconnectHandler = fn |
|
|
|
} |
|
|
|
|
|
|
|
func (m *Melody) HandleMessage(fn handleMessageFunc) { |
|
|
|
m.MessageHandler = fn |
|
|
|
// Callback when a message comes in.
|
|
|
|
func (m *Melody) HandleMessage(fn func(*Session, []byte)) { |
|
|
|
m.messageHandler = fn |
|
|
|
} |
|
|
|
|
|
|
|
// Fires when a session has an error.
|
|
|
|
func (m *Melody) HandleError(fn handleErrorFunc) { |
|
|
|
m.ErrorHandler = fn |
|
|
|
m.errorHandler = fn |
|
|
|
} |
|
|
|
|
|
|
|
// Handles a http request and upgrades it to a websocket.
|
|
|
|
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error { |
|
|
|
conn, err := m.Upgrader.Upgrade(w, r, nil) |
|
|
|
conn, err := m.upgrader.Upgrade(w, r, nil) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return err |
|
|
@ -68,25 +74,27 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error { |
|
|
|
|
|
|
|
m.hub.register <- session |
|
|
|
|
|
|
|
go m.ConnectHandler(session) |
|
|
|
go m.connectHandler(session) |
|
|
|
|
|
|
|
go session.writePump(m.ErrorHandler) |
|
|
|
go session.writePump(m.errorHandler) |
|
|
|
|
|
|
|
session.readPump(m.MessageHandler, m.ErrorHandler) |
|
|
|
session.readPump(m.messageHandler, m.errorHandler) |
|
|
|
|
|
|
|
m.hub.unregister <- session |
|
|
|
|
|
|
|
go m.DisconnectHandler(session) |
|
|
|
go m.disconnectHandler(session) |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// Broadcasts a message to all sessions.
|
|
|
|
func (m *Melody) Broadcast(msg []byte) { |
|
|
|
message := &envelope{t: websocket.TextMessage, msg: msg} |
|
|
|
m.hub.broadcast <- message |
|
|
|
} |
|
|
|
|
|
|
|
func (m *Melody) BroadcastFilter(fn filterFunc, msg []byte) { |
|
|
|
// Broadcasts a message to all sessions that fn returns true for.
|
|
|
|
func (m *Melody) BroadcastFilter(fn func(*Session) bool, msg []byte) { |
|
|
|
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn} |
|
|
|
m.hub.broadcast <- message |
|
|
|
} |