我一直在尝试让 grails 事件推送插件工作,但在savedToDo
调用事件时似乎无法从服务器获取响应数据。我可以在控制台中看到到达服务器并执行服务代码的数据的日志,但客户端似乎没有得到响应。我的代码如下:
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
}
}
索引.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>
我的问题是我没有看到控制台中记录了任何数据。有人知道我做错了什么吗?
编辑,我已经找出了问题所在,并相应地更新了 MyEvents.groovy,上面的代码现在可以工作了