1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-23 04:47:02 +00:00

add the new neffos StackExchange feature to the type aliases and shortcuts of the websocket subpackage and auto-enable debug mode on websocket MVC application when iris logger's level is set to debug

Former-commit-id: 4d8cb79d01a4172fc1ed7a9b626da0228d902b3c
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-07-11 12:59:11 +03:00
parent 82d645e3cb
commit 33dfb42d73
10 changed files with 83 additions and 57 deletions

View File

@@ -10,8 +10,6 @@ import (
"time"
"github.com/kataras/iris/websocket"
"github.com/kataras/neffos"
)
const (
@@ -23,17 +21,17 @@ const (
// this can be shared with the server.go's.
// `NSConn.Conn` has the `IsClient() bool` method which can be used to
// check if that's is a client or a server-side callback.
var clientEvents = neffos.Namespaces{
namespace: neffos.Events{
neffos.OnNamespaceConnected: func(c *neffos.NSConn, msg neffos.Message) error {
var clientEvents = websocket.Namespaces{
namespace: websocket.Events{
websocket.OnNamespaceConnected: func(c *websocket.NSConn, msg websocket.Message) error {
log.Printf("connected to namespace: %s", msg.Namespace)
return nil
},
neffos.OnNamespaceDisconnect: func(c *neffos.NSConn, msg neffos.Message) error {
websocket.OnNamespaceDisconnect: func(c *websocket.NSConn, msg websocket.Message) error {
log.Printf("disconnected from namespace: %s", msg.Namespace)
return nil
},
"chat": func(c *neffos.NSConn, msg neffos.Message) error {
"chat": func(c *websocket.NSConn, msg websocket.Message) error {
log.Printf("%s", string(msg.Body))
return nil
},
@@ -44,7 +42,7 @@ func main() {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
defer cancel()
client, err := neffos.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
client, err := websocket.Dial(ctx, websocket.DefaultGorillaDialer, endpoint, clientEvents)
if err != nil {
panic(err)
}

View File

@@ -6,8 +6,6 @@ import (
"github.com/kataras/iris"
"github.com/kataras/iris/websocket"
"github.com/kataras/neffos"
// Used when "enableJWT" constant is true:
"github.com/dgrijalva/jwt-go"
jwtmiddleware "github.com/iris-contrib/middleware/jwt"
@@ -17,10 +15,10 @@ import (
const enableJWT = true
const namespace = "default"
// if namespace is empty then simply neffos.Events{...} can be used instead.
var serverEvents = neffos.Namespaces{
namespace: neffos.Events{
neffos.OnNamespaceConnected: func(nsConn *neffos.NSConn, msg neffos.Message) error {
// if namespace is empty then simply websocket.Events{...} can be used instead.
var serverEvents = websocket.Namespaces{
namespace: websocket.Events{
websocket.OnNamespaceConnected: func(nsConn *websocket.NSConn, msg websocket.Message) error {
// with `websocket.GetContext` you can retrieve the Iris' `Context`.
ctx := websocket.GetContext(nsConn.Conn)
@@ -29,11 +27,11 @@ var serverEvents = neffos.Namespaces{
ctx.RemoteAddr())
return nil
},
neffos.OnNamespaceDisconnect: func(nsConn *neffos.NSConn, msg neffos.Message) error {
websocket.OnNamespaceDisconnect: func(nsConn *websocket.NSConn, msg websocket.Message) error {
log.Printf("[%s] disconnected from namespace [%s]", nsConn, msg.Namespace)
return nil
},
"chat": func(nsConn *neffos.NSConn, msg neffos.Message) error {
"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))
@@ -48,7 +46,7 @@ var serverEvents = neffos.Namespaces{
func main() {
app := iris.New()
websocketServer := neffos.New(
websocketServer := websocket.New(
websocket.DefaultGorillaUpgrader, /* DefaultGobwasUpgrader can be used too. */
serverEvents)
@@ -76,7 +74,7 @@ func main() {
//
// Check for token through the jwt middleware
// on websocket connection or on any event:
/* websocketServer.OnConnect = func(c *neffos.Conn) error {
/* websocketServer.OnConnect = func(c *websocket.Conn) error {
ctx := websocket.GetContext(c)
if err := j.CheckJWT(ctx); err != nil {
// will send the above error on the client