开发者

Faye and nodejs: How to run Faye server side client?

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


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);
});


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


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜