2017-02-15 07:40:43 +01:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
const (
|
|
|
|
// All is the string which the Emitter use to send a message to all
|
|
|
|
All = ""
|
|
|
|
// Broadcast is the string which the Emitter use to send a message to all except this connection
|
2017-07-10 17:32:42 +02:00
|
|
|
Broadcast = ";ionwebsocket;to;all;except;me;"
|
2017-02-15 07:40:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Emitter is the message/or/event manager
|
|
|
|
Emitter interface {
|
|
|
|
// EmitMessage sends a native websocket message
|
|
|
|
EmitMessage([]byte) error
|
|
|
|
// Emit sends a message on a particular event
|
|
|
|
Emit(string, interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
emitter struct {
|
|
|
|
conn *connection
|
|
|
|
to string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Emitter = &emitter{}
|
|
|
|
|
|
|
|
func newEmitter(c *connection, to string) *emitter {
|
|
|
|
return &emitter{conn: c, to: to}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *emitter) EmitMessage(nativeMessage []byte) error {
|
|
|
|
e.conn.server.emitMessage(e.conn.id, e.to, nativeMessage)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *emitter) Emit(event string, data interface{}) error {
|
2017-08-09 12:31:56 +02:00
|
|
|
if e.conn.server.rooms[e.to] == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:40:43 +01:00
|
|
|
message, err := websocketMessageSerialize(event, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.EmitMessage([]byte(message))
|
|
|
|
return nil
|
|
|
|
}
|