mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 20:07:04 +00:00
Version 3.0.0-beta cleaned
This commit is contained in:
50
websocket/emmiter.go
Normal file
50
websocket/emmiter.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package websocket
|
||||
|
||||
const (
|
||||
// All is the string which the Emmiter use to send a message to all
|
||||
All = ""
|
||||
// NotMe is the string which the Emmiter use to send a message to all except this connection
|
||||
NotMe = ";iris;to;all;except;me;"
|
||||
// Broadcast is the string which the Emmiter use to send a message to all except this connection, same as 'NotMe'
|
||||
Broadcast = NotMe
|
||||
)
|
||||
|
||||
type (
|
||||
// Emmiter is the message/or/event manager
|
||||
Emmiter interface {
|
||||
// EmitMessage sends a native websocket message
|
||||
EmitMessage([]byte) error
|
||||
// Emit sends a message on a particular event
|
||||
Emit(string, interface{}) error
|
||||
}
|
||||
|
||||
emmiter struct {
|
||||
conn *connection
|
||||
to string
|
||||
}
|
||||
)
|
||||
|
||||
var _ Emmiter = &emmiter{}
|
||||
|
||||
// emmiter implementation
|
||||
|
||||
func newEmmiter(c *connection, to string) *emmiter {
|
||||
return &emmiter{conn: c, to: to}
|
||||
}
|
||||
|
||||
func (e *emmiter) EmitMessage(nativeMessage []byte) error {
|
||||
mp := messagePayload{e.conn.id, e.to, nativeMessage}
|
||||
e.conn.server.messages <- mp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *emmiter) Emit(event string, data interface{}) error {
|
||||
message, err := serialize(event, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.EmitMessage([]byte(message))
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
Reference in New Issue
Block a user