mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user