I am a Grails newbie and working on getting WebSockets working in an application. I got most of it working except I couldn't figure out how to pass parameters to the methods annotated with @MessageMapping.
This works:
class MyController{
@MessageMapping(value="/start")
protected void startProcess(){ }
}
What I need something like this to work:
@MessageMapping(value="/start/{file}")
protected void startProcess(){
String file = params.file
//do somethig with the file...
}
But it doesn't work. I have tried changing UrlMappings.groovy, @PathVariable. I am pretty sure I am missing something simple. Any pointers?
To get something form the path use @DestinationVariable
(see 20.4.4 Annotation Message Handling in the spring websocket documentation).
Here is a working snippet (grails 2.4.3, based on the plugin example):
// Domain Class
class Foo {
String name
String desc
}
// controller method
@MessageMapping("/hello/{file}")
@SendTo("/topic/hello")
protected String hello(@DestinationVariable String file, @Payload Foo foo) {
return "received: ${file} ${foo}"
}
// javascript
client.send("/app/hello/FILE", {}, JSON.stringify({
'name': "foo",
'desc': "a foo"
}));