From 2495be17f650cdc7758e1d35669c337894afbc96 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 2 Jun 2019 18:27:51 +0300 Subject: [PATCH] add example line on how to get the upgraded conn's `iris.Context` with `websocket.GetContext`. Former-commit-id: 7d9cae97e2736d59dccb60c13b0cc5b6ca36a9fd --- _examples/websocket/basic/server.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/_examples/websocket/basic/server.go b/_examples/websocket/basic/server.go index 9c462dac..4b7508a6 100644 --- a/_examples/websocket/basic/server.go +++ b/_examples/websocket/basic/server.go @@ -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 }, },