2017-02-15 07:40:43 +01:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
const (
|
2017-12-20 16:56:28 +01:00
|
|
|
// All is the string which the Emitter use to send a message to all.
|
2017-02-15 07:40:43 +01:00
|
|
|
All = ""
|
2017-12-20 16:56:28 +01:00
|
|
|
// Broadcast is the string which the Emitter use to send a message to all except this connection.
|
|
|
|
Broadcast = ";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 {
|
2018-10-28 00:19:22 +02:00
|
|
|
message, err := e.conn.server.messageSerializer.serialize(event, data)
|
2017-02-15 07:40:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-28 00:19:22 +02:00
|
|
|
e.EmitMessage(message)
|
2017-02-15 07:40:43 +01:00
|
|
|
return nil
|
|
|
|
}
|