1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

Implement the websocket adaptor, a version of kataras/go-websocket, and refactor all of the previous websocket examples.

https://github.com/kataras/go-websocket/issues/27

Former-commit-id: 0b7e52e0a61150a8bba973ef653986d8b3ddd26b
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-02-15 08:40:43 +02:00
parent 26a5f9f3b0
commit 82afcc5aa6
30 changed files with 2692 additions and 330 deletions

View File

@@ -0,0 +1,49 @@
package websocket
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
// --------------------------------Emitter implementation-------------------------------
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
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 = ";gowebsocket;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 := websocketMessageSerialize(event, data)
if err != nil {
return err
}
e.EmitMessage([]byte(message))
return nil
}