|
@ -10,6 +10,7 @@ type handleErrorFunc func(*Session, error) |
|
|
type handleSessionFunc func(*Session) |
|
|
type handleSessionFunc func(*Session) |
|
|
type filterFunc func(*Session) bool |
|
|
type filterFunc func(*Session) bool |
|
|
|
|
|
|
|
|
|
|
|
// Melody implements a websocket manager.
|
|
|
type Melody struct { |
|
|
type Melody struct { |
|
|
Config *Config |
|
|
Config *Config |
|
|
Upgrader *websocket.Upgrader |
|
|
Upgrader *websocket.Upgrader |
|
@ -22,7 +23,7 @@ type Melody struct { |
|
|
hub *hub |
|
|
hub *hub |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Returns a new melody instance with default Upgrader and Config.
|
|
|
|
|
|
|
|
|
// New creates a new melody instance with default Upgrader and Config.
|
|
|
func New() *Melody { |
|
|
func New() *Melody { |
|
|
upgrader := &websocket.Upgrader{ |
|
|
upgrader := &websocket.Upgrader{ |
|
|
ReadBufferSize: 1024, |
|
|
ReadBufferSize: 1024, |
|
@ -46,37 +47,37 @@ func New() *Melody { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fires fn when a session connects.
|
|
|
|
|
|
|
|
|
// HandleConnect fires fn when a session connects.
|
|
|
func (m *Melody) HandleConnect(fn func(*Session)) { |
|
|
func (m *Melody) HandleConnect(fn func(*Session)) { |
|
|
m.connectHandler = fn |
|
|
m.connectHandler = fn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fires fn when a session disconnects.
|
|
|
|
|
|
|
|
|
// HandleDisconnect fires fn when a session disconnects.
|
|
|
func (m *Melody) HandleDisconnect(fn func(*Session)) { |
|
|
func (m *Melody) HandleDisconnect(fn func(*Session)) { |
|
|
m.disconnectHandler = fn |
|
|
m.disconnectHandler = fn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fires fn when a pong is received from a session.
|
|
|
|
|
|
|
|
|
// HandlePong fires fn when a pong is received from a session.
|
|
|
func (m *Melody) HandlePong(fn func(*Session)) { |
|
|
func (m *Melody) HandlePong(fn func(*Session)) { |
|
|
m.pongHandler = fn |
|
|
m.pongHandler = fn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Callback when a text message comes in.
|
|
|
|
|
|
|
|
|
// HandleMessage fires fn when a text message comes in.
|
|
|
func (m *Melody) HandleMessage(fn func(*Session, []byte)) { |
|
|
func (m *Melody) HandleMessage(fn func(*Session, []byte)) { |
|
|
m.messageHandler = fn |
|
|
m.messageHandler = fn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Callback when a binary message comes in.
|
|
|
|
|
|
|
|
|
// HandleMessageBinary fires fn when a binary message comes in.
|
|
|
func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) { |
|
|
func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) { |
|
|
m.messageHandlerBinary = fn |
|
|
m.messageHandlerBinary = fn |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Fires 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 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Handles http requests and upgrades them to websocket connections.
|
|
|
|
|
|
|
|
|
// HandleRequest upgrades http requests to websocket connections and dispatches them to be handled by the melody instance.
|
|
|
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) { |
|
|
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) { |
|
|
conn, err := m.Upgrader.Upgrade(w, r, nil) |
|
|
conn, err := m.Upgrader.Upgrade(w, r, nil) |
|
|
|
|
|
|
|
@ -107,45 +108,45 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) { |
|
|
go m.disconnectHandler(session) |
|
|
go m.disconnectHandler(session) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a text message to all sessions.
|
|
|
|
|
|
|
|
|
// Broadcast broadcasts a text message to all sessions.
|
|
|
func (m *Melody) Broadcast(msg []byte) { |
|
|
func (m *Melody) Broadcast(msg []byte) { |
|
|
message := &envelope{t: websocket.TextMessage, msg: msg} |
|
|
message := &envelope{t: websocket.TextMessage, msg: msg} |
|
|
m.hub.broadcast <- message |
|
|
m.hub.broadcast <- message |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a text message to all sessions that fn returns true for.
|
|
|
|
|
|
|
|
|
// BroadcastFilter broadcasts a text message to all sessions that fn returns true for.
|
|
|
func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) { |
|
|
func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) { |
|
|
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn} |
|
|
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn} |
|
|
m.hub.broadcast <- message |
|
|
m.hub.broadcast <- message |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a text message to all sessions except session s.
|
|
|
|
|
|
|
|
|
// BroadcastOthers broadcasts a text message to all sessions except session s.
|
|
|
func (m *Melody) BroadcastOthers(msg []byte, s *Session) { |
|
|
func (m *Melody) BroadcastOthers(msg []byte, s *Session) { |
|
|
m.BroadcastFilter(msg, func(q *Session) bool { |
|
|
m.BroadcastFilter(msg, func(q *Session) bool { |
|
|
return s != q |
|
|
return s != q |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a binary message to all sessions.
|
|
|
|
|
|
|
|
|
// BroadcastBinary broadcasts a binary message to all sessions.
|
|
|
func (m *Melody) BroadcastBinary(msg []byte) { |
|
|
func (m *Melody) BroadcastBinary(msg []byte) { |
|
|
message := &envelope{t: websocket.BinaryMessage, msg: msg} |
|
|
message := &envelope{t: websocket.BinaryMessage, msg: msg} |
|
|
m.hub.broadcast <- message |
|
|
m.hub.broadcast <- message |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a binary message to all sessions that fn returns true for.
|
|
|
|
|
|
|
|
|
// BroadcastBinaryFilter broadcasts a binary message to all sessions that fn returns true for.
|
|
|
func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) { |
|
|
func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) { |
|
|
message := &envelope{t: websocket.BinaryMessage, msg: msg, filter: fn} |
|
|
message := &envelope{t: websocket.BinaryMessage, msg: msg, filter: fn} |
|
|
m.hub.broadcast <- message |
|
|
m.hub.broadcast <- message |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Broadcasts a binary message to all sessions except session s.
|
|
|
|
|
|
|
|
|
// BroadcastBinaryOthers broadcasts a binary message to all sessions except session s.
|
|
|
func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) { |
|
|
func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session) { |
|
|
m.BroadcastBinaryFilter(msg, func(q *Session) bool { |
|
|
m.BroadcastBinaryFilter(msg, func(q *Session) bool { |
|
|
return s != q |
|
|
return s != q |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Closes the melody instance and all connected sessions.
|
|
|
|
|
|
|
|
|
// Close closes the melody instance and all connected sessions.
|
|
|
func (m *Melody) Close() { |
|
|
func (m *Melody) Close() { |
|
|
m.hub.exit <- true |
|
|
m.hub.exit <- true |
|
|
} |
|
|
} |