问题
I have been trying to get the grails events push plugin to work but cant seem to get response data from the server when the savedToDo
event is called. I can see the logs in my console for the data coming to the server and executing the service code, but the client doesnt seem to be getting back a response. My code is as follows :
MyService.groovy
package eventspush
import grails.converters.JSON
import grails.events.*
class MyService {
//will receive client events from 'saveTodo' topic
@Listener(namespace='browser')
def saveTodo(Map data){
log.info(data)
data.moreData = "There we go...."
event('savedTodo', data) // will trigger registered browsers on 'savedTodo' topic
}
}
conf/MyEvents.groovy
events = {
"savedTodo" browser: true, browserFilter: { message, request ->
return true
}
}
index.gsp
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<r:require modules="grailsEvents"/>
<meta name='layout' content='main'/>
<r:script>
$(document).ready(function () {
/*
Register a grailsEvents handler for this window, constructor can take a root URL,
a path to event-bus servlet and options. There are sensible defaults for each argument
*/
window.grailsEvents = new grails.Events("${createLink(uri: '')}", {logLevel:"debug", transport:'websocket'});
var data = new Object();
data.name="some name"
grailsEvents.send('saveTodo', data); //will send data to server topic 'saveTodo'
grailsEvents.on('savedTodo', function (data) {
console.log(data)
}, {});
});
</r:script>
</head>
My problem is that I am not seeing any data being logged in the console. Does anybody have any idea what I am doing wrong?
Edit, I have worked out what was wrong and I have updated the MyEvents.groovy accordingly, the above code is now working
回答1:
It seems that you have added the 'savedTodo' mapping to the PushEvents.groovy
file within the Plugin
directory. This is wrong!
You should add your own *Events.groovy
file within the conf
folder of your own project and leave the PushEvents.groovy
file of the plugin untouched!
The other code looks fine so far ;)
There's a typo within your JavaScript section in index.gsp
:
it reads:
grailsEvents.send('saveTodo', data);
when it should be:
grailsEvents.send('savedTodo', data);
you omitted the "d" ... ;)
回答2:
So I found out what what was causing the data not being pushed to the client.
I edited the line in MyService.groovy
from being
event('savedTodo', data)
to
event(topic:'savedTodo', data:data)
It is now working as expected
来源:https://stackoverflow.com/questions/14943049/grails-events-push-plugin-not-getting-a-response-from-server-event