@MessageMapping with Grails spring-websocket plugin

232 views
Skip to first unread message

vgoud

unread,
Aug 1, 2014, 3:07:39 PM8/1/14
to grails-de...@googlegroups.com
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:

<!-- language: lang-groovy -->
`class MyController{
    @MessageMapping(value="/start")
    protected void startProcess(){ }
}
`

What I need something like this to work:

<!-- language: lang-java -->
`
     @MessageMapping(value="/start/{file}")
     protected void startProcess(){ 
         String file = params.file
         //do somethig with the file...
     }
`
But it doesn't work. I have tried playing with UrlMappings.groovy, @PathVariable. I am pretty sure I am missing something simple. Any pointers?

zyro

unread,
Aug 1, 2014, 3:17:33 PM8/1/14
to grails-de...@googlegroups.com
hi,

check

http://docs.spring.io/spring/docs/4.0.6.RELEASE/spring-framework-reference/htmlsingle/#websocket-stomp-handle-annotations

sth. like this should work:

@MessageMapping("/start/{file}")
protected void startProcess(@DestinationVariable String file){
//do somethig with the file...
}

do not hesitate to post back if you are facing difficulties.

zyro
> --
> You received this message because you are subscribed to the Google
> Groups "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to grails-dev-disc...@googlegroups.com
> <mailto:grails-dev-disc...@googlegroups.com>.
> To post to this group, send email to grails-de...@googlegroups.com
> <mailto:grails-de...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/3e6d0880-f7ef-4aeb-99bd-b03dc2c55ddd%40googlegroups.com
> <https://groups.google.com/d/msgid/grails-dev-discuss/3e6d0880-f7ef-4aeb-99bd-b03dc2c55ddd%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

vgoud

unread,
Aug 4, 2014, 5:53:18 PM8/4/14
to grails-de...@googlegroups.com
Thank you. That works, most of the time.. 
I am noticing a strange behavior where it seems to stop and start working randomly. I have spent a whole day trying to isolate the problem but no luck. It's been very frustrating. 


I see this in the log.

support.SimpAnnotationMethodMessageHandler Handling message, lookupDestination=/start/MY_FILE

support.SimpAnnotationMethodMessageHandler No matching method found

zyro

unread,
Aug 5, 2014, 1:30:02 PM8/5/14
to grails-de...@googlegroups.com
may i ask what type the actual payload of your message is?
i think there are a few more options to achieve passing in that filename
which do not rely on dynamic destination mapping, e.g.
- the filename as the payload itself
- the filename as a part of a json object thats the payload
- the filename as a message header
> > an email to grails-dev-disc...@googlegroups.com
> <javascript:>
> > <mailto:grails-dev-disc...@googlegroups.com
> <javascript:>>.
> > To post to this group, send email to
> grails-de...@googlegroups.com <javascript:>
> > <mailto:grails-de...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/grails-dev-discuss/3e6d0880-f7ef-4aeb-99bd-b03dc2c55ddd%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Grails Dev Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to grails-dev-disc...@googlegroups.com
> <mailto:grails-dev-disc...@googlegroups.com>.
> To post to this group, send email to grails-de...@googlegroups.com
> <mailto:grails-de...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grails-dev-discuss/8bcac483-beef-40a6-8592-40832a026625%40googlegroups.com
> <https://groups.google.com/d/msgid/grails-dev-discuss/8bcac483-beef-40a6-8592-40832a026625%40googlegroups.com?utm_medium=email&utm_source=footer>.

Goud, Venkat

unread,
Aug 6, 2014, 2:09:57 PM8/6/14
to grails-de...@googlegroups.com
It is an absolute path to a file. After playing around with regular
expressions without much luck, I am finally using this approach:

//file is URLencoded
@MessageMapping("/start?f={file}²)
protected def start(@DestinationVariable String file) {

logFile = URLDecoder.decode(file, "UTF-8²);
}

I guess I could have put the file name in the payload and use it without
the encode and decode.
>email to grails-dev-disc...@googlegroups.com.
>To post to this group, send email to grails-de...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/grails-dev-discuss/53E11493.9020509%40zy
>ro.net.

zyro

unread,
Aug 6, 2014, 2:45:07 PM8/6/14
to grails-de...@googlegroups.com
yea if its only about the filename, i would opt for using the payload
itself.

@MessageMapping("/start")
protected void start(String file) {
println file
}

just note that although it is just a string, it needs to be valid json
for the transport, i.e. use JSON.stringify(fileName) when sending from
javascript.

zyro

unread,
Aug 6, 2014, 2:46:29 PM8/6/14
to grails-de...@googlegroups.com
filename meaning your mentioned absolute path.
Reply all
Reply to author
Forward
0 new messages