我是Grails的新手,致力于让WebSockets在应用程序中工作。我完成了大部分工作,但我不知道如何将参数传递给带有@MessageMapping注解的方法。
这样做是可行的:
class MyController{
@MessageMapping(value="/start")
protected void startProcess(){ }
}我需要这样的东西来发挥作用:
@MessageMapping(value="/start/{file}")
protected void startProcess(){
String file = params.file
//do somethig with the file...
}但不起作用。我试过改变UrlMappings.groovy,@PathVariable。我很确定我错过了一些简单的东西。有什么指示吗?
发布于 2014-08-02 14:15:20
要从路径中获得一些信息,请使用@DestinationVariable (参见20.4.4,在弹簧websocket文档中注释消息处理)。
下面是一个工作片段(grails2.4.3,基于插件示例):
// 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"
}));https://stackoverflow.com/questions/25085858
复制相似问题