mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
add example line on how to get the upgraded conn's iris.Context
with websocket.GetContext
.
Former-commit-id: 7d9cae97e2736d59dccb60c13b0cc5b6ca36a9fd
This commit is contained in:
parent
04bc21dd3b
commit
2495be17f6
|
@ -12,21 +12,26 @@ const namespace = "default"
|
|||
// if namespace is empty then simply websocket.Events{...} can be used instead.
|
||||
var serverEvents = websocket.Namespaces{
|
||||
namespace: websocket.Events{
|
||||
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] connected to namespace [%s]", c, msg.Namespace)
|
||||
websocket.OnNamespaceConnected: func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] connected to namespace [%s] with IP [%s]",
|
||||
nsConn, msg.Namespace,
|
||||
// with `GetContext` you can retrieve the Iris' `Context`, alternatively
|
||||
// you can use the `nsConn.Conn.Socket().Request()` to get the raw `*http.Request`.
|
||||
websocket.GetContext(nsConn.Conn).RemoteAddr())
|
||||
return nil
|
||||
},
|
||||
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] disconnected from namespace [%s]", c, msg.Namespace)
|
||||
websocket.OnNamespaceDisconnect: func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] disconnected from namespace [%s]", nsConn, msg.Namespace)
|
||||
return nil
|
||||
},
|
||||
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
|
||||
log.Printf("[%s] sent: %s", c.Conn.ID(), string(msg.Body))
|
||||
"chat": func(nsConn *websocket.NSConn, msg websocket.Message) error {
|
||||
// room.String() returns -> NSConn.String() returns -> Conn.String() returns -> Conn.ID()
|
||||
log.Printf("[%s] sent: %s", nsConn, string(msg.Body))
|
||||
|
||||
// Write message back to the client message owner with:
|
||||
// c.Emit("chat", msg)
|
||||
// nsConn.Emit("chat", msg)
|
||||
// Write message to all except this client with:
|
||||
c.Conn.Server().Broadcast(c, msg)
|
||||
nsConn.Conn.Server().Broadcast(nsConn, msg)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user