【问题标题】:@MessageMapping with Grails spring-websocket plugin@MessageMapping 与 Grails spring-websocket 插件
【发布时间】:2014-08-01 17:46:56
【问题描述】:

我是 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。我很确定我错过了一些简单的东西。有什么指点吗?

【问题讨论】:

    标签: grails grails-2.0 grails-controller spring-websocket


    【解决方案1】:

    要从路径中获取某些内容,请使用@DestinationVariable(请参阅spring websocket 文档中的20.4.4 注释消息处理)。

    这是一个有效的 sn-p(grails 2.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"
    }));
    

    【讨论】:

    • 谢谢。这样可行。但是我注意到一个奇怪的行为,它有时会停止工作,并且没有任何代码更改就可以重新开始工作。
    • hii.. 多重变量怎么样? /hello/{file}/{mycode}??
    • @Martin Hauner hii .. multipe 变量怎么样? /hello/{file}/{mycode} ??
    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多