zyro23 / grails-spring-websocket

93 stars 28 forks source link

lookupDestination mapping does not work by default in grails controller #70

Closed aabjava closed 3 years ago

aabjava commented 3 years ago

using: implementation "org.grails.plugins:grails-spring-websocket:2.4.1"

with: grails 3.3.1 os win 10 java se 1.8.0_112

Test case in https://github.com/aabjava/TWebsockets

import org.springframework.stereotype.Controller

@Controller
class WebsocketController {

    @MessageMapping("requestcontroller")
    @SendTo("replycontroller")
    protected String reply(){
        return "This is a websocket reply"
    }
}

This works and can be seen in console log output, message handlers are found in class

class Websocket2Controller {

    @MessageMapping("requestcontroller2")
    @SendTo("replycontroller2")
    protected String reply(){
        return "This is a websocket reply2"
    }
}

Above does not work and method mappings are ignored.

I actually think this is better than scanning all the controllers methods for mapping (what appears to be the case) , but this is not stated in the docs and can be confusing. Also i don't know if using org.springframework.stereotype.Controller in a grails controller will break any of the default for grails controllers it does not seem to

zyro23 commented 3 years ago

cannot reproduce.

added logger("grails.plugin.springwebsocket", DEBUG, ["STDOUT"], false) to your sample app.

console out:

 DEBUG --- [           main] GrailsSimpAnnotationMethodMessageHandler : 1 message handler methods found on class com.test.Websocket2Controller: {protected java.lang.String com.test.Websocket2Controller.reply()={[/requestcontroller2],messageType=[MESSAGE]}}
  INFO --- [           main] GrailsSimpAnnotationMethodMessageHandler : Mapped "{[/requestcontroller2],messageType=[MESSAGE]}" onto protected java.lang.String com.test.Websocket2Controller.reply()

by default all grails controllers will be scanned. https://github.com/zyro23/grails-spring-websocket/tree/2.4.x#usage:

Those annotations can be used in:

  • Regular Grails controllers
  • WebSocket Grails artefacts (grails create-web-socket my.package.name.MyWebSocket)
  • Spring @Controller beans

if thats an issue for you, refer to https://github.com/zyro23/grails-spring-websocket/tree/2.4.x#startup-performance

can this be closed?

aabjava commented 3 years ago

Yes it can.

Thanks for the quick response,

I now cannot reproduce either, don't know what happened.

Updated sample, now controller that has the spring annotation is called twice for the topic, but i guess that makes sense.

Thanks