2013-12-11 47 views
3

我們希望創建一個聊天應用程序,用戶在其中輸入一些文本,然後其他用戶看到該應用程序。 Ext.Ajax.request是被調用來通過Ajax發送數據到服務器的函數。服務器使用Grails發送事件

這是煎茶建築師3的代碼:

var panel = button.up(); 
var input = panel.getComponent("inputField"); 
console.debug("Message: " + input.getValue()); 


Ext.Ajax.request({ 
    url: '/Chat/chatMain/send', 
    params: { 
     message: input.getValue() 
    }, 
    success: function(response){ 
     console.log("Message successfully send."); 
    }, 
    failure: function(response){ 
     console.log("ERROR!"); 
     console.log('server-side failure with status code ' + response.status); 
    } 
}); 

聊天控制器Grails中:

下面例子將收到服務器發送將從Grails應用程序來發送事件:

var source = new EventSource('/Chat/chatMain/receive'); 

source.addEventListener('sms', showSms, false); 

source.onmessage = function (event) { 
    // a message without a type was fired 
    showSms(event); 
}; 

function showSms(e){ 
    console.log(e.data); 
} 

我們發現grails插件的氛圍和事件推動,但例子只顯示websoc kets不是服務器端事件。我們不知道如何使用事件推送插件從grails服務器傳播文本。任何人都可以幫忙嗎?

+2

這是事件推送插件的[示例](https://github.com/alidadasb/eventPushExample)。它可能有幫助。 – Alidad

+0

關於Grails的郵件列表有很長的討論。也許從那裏的信息可以幫助:http://grails.1312388.n4.nabble.com/Server-Push-HowTo-td4641344.html – matejk

回答

0

您可以在Grails服務中發送您的服務器事件。 The plugin page show's an example

MyService.groovy

//will receive client events from 'saveTodo' topic 
@Listener(namespace='browser') saveTodo(Map data){ 
    //... 
    // will trigger registered browsers on 'savedTodo' topic 
    event([namespace: 'browser', topic: 'savedTodo', data: data]) 
} 

要在需要導入插件模塊,客戶端收到此:

<r:require module="grailsEvents"/> 
<r:script> 
    var grailsEvents = new grails.Events("http://localhost:8080/app/"); 
    grailsEvents.send('saveTodo', data); //will send data to server topic 'saveTodo' 
    //will listen for server events on 'savedTodo' topic 
    grailsEvents.on('savedTodo', function(data){...}); 
</r:script> 

data參數是從服務器事件的響應。

請務必檢查文檔鏈接中的Tomcat配置,這是重要的一步。