iris/websocket/emitter.go
Gerasimos (Makis) Maropoulos be32418bc1 Update to version 11.0.1. Feature request implemented: https://github.com/kataras/iris/issues/1113
Former-commit-id: 0ce38dbacc2458fe327fa4401fdde1e69c8aacb0
2018-10-28 01:19:22 +03:00

44 lines
994 B
Go

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.
Broadcast = ";to;all;except;me;"
)
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 {
message, err := e.conn.server.messageSerializer.serialize(event, data)
if err != nil {
return err
}
e.EmitMessage(message)
return nil
}