Faye 和 Nodejs:如何运行 Faye 服务器端客户端?

发布于 2024-11-08 12:33:22 字数 95 浏览 0 评论 0原文

我正在尝试开发一个 Faye 服务器端客户端以根据需要自动运行。在Faye的官方网站上,我只找到了关于服务器端客户端的文档,没有关于如何运行它的信息。 请告诉我该怎么做 谢谢

I'm trying to develop a Faye server side client to run automatically as needed. In the official website of Faye, I find only document about server side client, there is no information about how to run it.
Please tell me how to do so
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-11-15 12:33:22

文档中缺少一个关键部分。看来您需要调用 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);
});

There's a key missing piece in the documentation. It appears you need to call client.connect() in order to receive events.

Here is what worked for me:

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);
});
滴情不沾 2024-11-15 12:33:22

刚刚通过谷歌偶然发现了这一点。您应该能够仅使用以下代码运行客户端:

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

just stumpled on this through Google. You should be able to run a client using just this code:

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

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

If you still have trouble, please get on the mailing list at http://groups.google.com/group/faye-users

酒解孤独 2024-11-15 12:33:22

创建一个 .rb 文件并填充以下代码

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

转到控制台并键入

ruby your_file.rb 

Once you do.使用 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>

我想你已经准备好了。 :)

Create a .rb file and fill with following code

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

Go to console and type

ruby your_file.rb 

Once you have done. create a js with html as following :

<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>

And I think u r ready to go. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文