2011-05-19 126 views
3

我想開發一個Faye服務器端客戶端,根據需要自動運行。在Faye的官方網站上,我只找到關於服務器端客戶端的文檔,沒有關於如何運行它的信息。 請告訴我怎麼做 謝謝Faye和nodejs:如何運行Faye服務器端客戶端?

+0

使用'http.request'只創建一個HTTP請求到節點的王菲服務器。 – Raynos 2011-05-19 11:36:20

回答

2

只是通過谷歌這一點。你應該能夠運行只使用此代碼客戶端:

var faye = require('faye'), 
    client = new faye.Client('http://example.com/faye'); 

client.subscribe('/some/channel', function(message) { 
    // process message 
}); 

如果您仍然有問題,請在郵件列表上得到的http://groups.google.com/group/faye-users

+0

這實際上不起作用。它缺少'client.connect()'調用。詳情請參閱我的回答。 – Micah 2013-12-05 15:40:42

0

創建一個名爲.rb文件,並與下面的代碼

填寫
require 'rubygems' 
require 'faye' 
cliv=Faye::RackAdapter.new(
    :mount => '/cliv', 
    :timeout => 25 
) 
cliv.listen(3000) 

進入控制檯,輸入

ruby your_file.rb 

一旦你做東北。用html創建一個js如下:

<script type="text/javascript" src="http://localhost:3000/faye.js"></script> 
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js'></script> 
<script> 
$("document").ready(function(){ 
    faye = new Faye.Client('http://localhost:3000/faye'); 
    faye.connect(); 
    subscribeObj = faye.subscribe("/hi", function(message) { 
    $("#response").append(message.text); 
    $("#content").val(""); 
    }); 

    $("#say").click(function(){ 
    content=$("#content").val(); 
    faye.publish("/hi", {text: content}); 
    }); 
}); 
</script> 
<div id='response'></div> 
<br/> 
<input type='text' id='content' /> 
<div style='cursor:pointer;' id='say'> Say Something </div> 

我想你準備好了。 :)

+0

他要求node.js的 – 2012-10-15 19:45:38

2

在文檔中有一個關鍵的缺失部分。看來你需要撥打client.connect()才能收到活動。

這裏是我工作:

var faye = require('faye'); 

var client = new faye.Client('http://localhost:8000/faye'); 

//This was missing from the documentation 
client.connect(); 

var subscription = client.subscribe('/foo', function(message){ 
    console.log("Event Received"); 
    console.log(message); 
}) 

//This is optional 
subscription.then(function() { 
    console.log('Subscription is now active!'); 
}); 

var publication = client.publish('/foo', {text: "Hello World!"}); 

publication.then(function() { 
    console.log('Message received by server!'); 
}, function(error) { 
    console.log('There was a problem: ' + error.message); 
}); 
+0

好了,我在這個初學者,我不知道太多關於JavaScript。但是確實需要('faye')這意味着它需要一個名爲'faye'的文件或什麼?如果是,那麼在哪裏獲取文件? – tyegah123 2014-05-03 21:23:25