iris/websocket/emitter.go

51 lines
1.1 KiB
Go
Raw Normal View History

2016-05-30 16:08:09 +02:00
package websocket
const (
// All is the string which the Emitter use to send a message to all
2016-05-30 16:08:09 +02:00
All = ""
// NotMe is the string which the Emitter use to send a message to all except this connection
2016-05-30 16:08:09 +02:00
NotMe = ";iris;to;all;except;me;"
// Broadcast is the string which the Emitter use to send a message to all except this connection, same as 'NotMe'
2016-05-30 16:08:09 +02:00
Broadcast = NotMe
)
type (
// Emitter is the message/or/event manager
Emitter interface {
2016-05-30 16:08:09 +02:00
// EmitMessage sends a native websocket message
EmitMessage([]byte) error
// Emit sends a message on a particular event
Emit(string, interface{}) error
}
emitter struct {
2016-05-30 16:08:09 +02:00
conn *connection
to string
}
)
var _ Emitter = &emitter{}
2016-05-30 16:08:09 +02:00
// emitter implementation
2016-05-30 16:08:09 +02:00
func newEmitter(c *connection, to string) *emitter {
return &emitter{conn: c, to: to}
2016-05-30 16:08:09 +02:00
}
func (e *emitter) EmitMessage(nativeMessage []byte) error {
2016-05-30 16:08:09 +02:00
mp := messagePayload{e.conn.id, e.to, nativeMessage}
e.conn.server.messages <- mp
return nil
}
func (e *emitter) Emit(event string, data interface{}) error {
2016-05-30 16:08:09 +02:00
message, err := serialize(event, data)
if err != nil {
return err
}
e.EmitMessage([]byte(message))
return nil
}
//