我是faye web套接字的新手,我试图设置服务器和客户端来交换信息。不幸的是,我错过了一些东西,我不能在客户端从服务器发送的消息。这是我的server.js:
var faye = require('faye');
var Router = require('node-simple-router');
var router = new Router();
var server = require('http').createServer(router);
var port = 5000;
var bayeux = new faye.NodeAdapter({mount: '/faye/calls'});
bayeux.attach(server);
// Launch the server
server.listen(port, function() {
console.log('Listening on ' + port);
setInterval(function(){
bayeux.getClient().publish('/faye/calls', {text: 'Hello'});
},3000);
And is my client.js
var faye = require('faye');
var client = new faye.Client('http://localhost:5000/faye', {
timeout: 600,
retry: 5
});
var sub = client.subscribe('/calls', function(message) {
console.log('message:' + message);
});
sub.then(function() {
console.log('overall Subscription is now active!');
});
谁能告诉我我错过了什么?非常感谢
祝你过得愉快
看起来你的路径是错的:
var bayeux = new faye.NodeAdapter({mount: '/faye/calls'});
应该挂载到/faye
:
var bayeux = new faye.NodeAdapter({mount: '/faye'});
和
bayeux.getClient().publish('/faye/calls', {text: 'Hello'});
实际上应该发布到/calls
bayeux.getClient().publish('/calls', {text: 'Hello'});
这似乎为我解决了问题:
overall Subscription is now active!
message:[object Object]
message:[object Object]
message:[object Object]