我正在使用 node js 和 faye 简单地将一些消息传递给客户端,
我创建一个节点服务器
var http = require('http'),
faye = require('faye'),
url = require('url'),
qs = require('querystring');
var POST;
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
function publish(request,response)
{
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
POST = qs.parse(body);
if(POST.secrete_key=="@#$werw*#@erwe*&^&*rw234234") // validate request using secret key
{
if(POST.root=="global"||POST.root=="web"){
bayeux.getClient().publish(POST.channelWeb,{text: POST.textWeb});
}
if(POST.root=="global"||POST.root=="mobile"){
bayeux.getClient().publish(POST.channelMobile,{text: POST.textMobile});
}
//eval(POST.auth_type+"_"+POST.update_type+"()");
}//end validate request
else
{
response.writeHead(404);
response.end('404 File not found');
}
});
response.end();
}
// Handle non-Bayeux requests
var server = http.createServer(function (request,response)
{
var pathRegex = new RegExp('^/publish/?$');
var pathname = url.parse(request.url).pathname;
if (pathRegex.test(pathname)) {
publish(request, response);
} else {
render404(request, response);
}
});
bayeux.attach(server);
server.listen(8000);
我用来bayeux.getClient().publish(
向特定客户端发布消息。
我创建了一个订阅 js
var client = new Faye.Client(n.node_url+':8000/faye/');
client.subscribe(n.channel, function(message) {
obj.processNotification(obj,message.text,n.user_id,n.user_type,n.channel);
});
问题是,我不知道如何创建频道
在
bayeux.getClient().publish(channel, message);
以及如何订阅它,请帮助。提前致谢 ................