kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.26k stars 2.47k forks source link

[WebsocketMVC]call of reflect.Value.NumField on ptr Value #1563

Closed rxrw closed 4 years ago

rxrw commented 4 years ago

When I code as the wiki (https://github.com/kataras/iris/wiki/Websockets#websocket-controller) I got an error..

package main

import (
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/middleware/logger"
    "github.com/kataras/iris/v12/mvc"
    "github.com/kataras/iris/v12/websocket"
    "github.com/kataras/neffos"

    "os"
)

func main() {
    app := iris.New()
    app.Logger().SetLevel("debug")
    app.Use(logger.New())

    mvcApp := mvc.New(app.Party("/ws"))
    mvcApp.HandleWebsocket(&websocketController{Namespace: "default"})
    websocketServer := neffos.New(websocket.DefaultGorillaUpgrader, mvcApp)
    mvcApp.Router.Get("/", websocket.Handler(websocketServer))
    err := app.Listen(":8080")
    if err != nil {
        println(err)
        os.Exit(-1)
    }
}

type websocketController struct {
    *neffos.NSConn `stateless:"true"`
    Namespace string

}

func (c *websocketController) OnNamespaceConnected(msg neffos.Message) error {
    return nil
}

func (c *websocketController) OnNamespaceDisconnect(msg neffos.Message) error {
    return nil
}

func (c *websocketController) OnChat(msg neffos.Message) error {
    return nil
}

The error was

/private/var/folders/7t/qrk4p1sx07n9m_jly6rkmt7m0000gn/T/___go_build_iris_learn #gosetup
panic: reflect: call of reflect.Value.NumField on ptr Value

goroutine 1 [running]:
reflect.flag.mustBe(...)
        /usr/local/Cellar/go/1.14.3/libexec/src/reflect/value.go:208
reflect.Value.NumField(0x18d1de0, 0xc000225680, 0x196, 0x1a94d60)
        /usr/local/Cellar/go/1.14.3/libexec/src/reflect/value.go:1346 +0xbe
github.com/kataras/iris/v12/hero.lookupFields(0x18d1de0, 0xc000225680, 0x196, 0x1, 0xc0005013e0, 0x1, 0x1, 0x1, 0x0, 0xc0005013d8)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/reflect.go:120 +0xa2
github.com/kataras/iris/v12/hero.lookupFields(0x18e78e0, 0xc000225680, 0x199, 0x1040001, 0x0, 0x0, 0x0, 0xc000225be0, 0xc0004e99c8, 0x10fd4b3)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/reflect.go:127 +0x58f
github.com/kataras/iris/v12/hero.lookupNonZeroFieldValues(0x18e78e0, 0xc000225680, 0x199, 0x18e78e0, 0xc000225680, 0x199)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/reflect.go:156 +0x75
github.com/kataras/iris/v12/hero.getBindingsForStruct(0x18e30a0, 0xc000225680, 0x16, 0xc0002478f0, 0x8, 0xe, 0x0, 0x197e5a8, 0x100e1d6, 0xc000225c00, ...)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/binding.go:250 +0xea
github.com/kataras/iris/v12/hero.makeStruct(0x191d480, 0xc000225c00, 0xc000079000, 0x0, 0xf)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/struct.go:54 +0x12a
github.com/kataras/iris/v12/hero.(*Container).Struct(...)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/hero/container.go:193
github.com/kataras/iris/v12/mvc.(*ControllerActivator).attachInjector(0xc00007a840)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/mvc/controller.go:259 +0x179
github.com/kataras/iris/v12/mvc.(*ControllerActivator).markAsWebsocket(...)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/mvc/controller.go:253
github.com/kataras/iris/v12/mvc.(*Application).HandleWebsocket(0xc00007a780, 0x18e30a0, 0xc000225680, 0x0)
        /Users/i/go/pkg/mod/github.com/kataras/iris/v12@v12.1.9-0.20200618063647-c11725ab44d1/mvc/mvc.go:218 +0x6c
main.main()
        /Users/i/Code/go/iris-learn/main.go:27 +0x1fc

Process finished with exit code 2

Have I done any mistake? Thanks for your help :-D

kataras commented 4 years ago

@reprover I literally didn't see that one, it's fixed. I have no words to thank you for that!