如何创build视图的订阅path,发送到由bayeux.getClient()发布的消息publish(

我使用节点js和faye来传递一些消息给客户端,

我创build一个节点服务器

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(发布消息到特定的客户端。

我已经创build了一个订阅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); }); 

问题是,我不知道如何创build频道

 bayeux.getClient().publish(channel, message); 

以及如何订阅它,请帮助。 提前致谢 …………….

您不会创build频道,也不需要事先设置,只需发布​​到频道,任何位于该频道的收听者都将收到该数据。

您已经拥有在您的代码中订阅该通道的代码:

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

基本上在服务器端,您可以创build一个创build不同渠道的逻辑,并将其保存在您的数据库中,供客户订阅,并使用相同的通信。

例如,可能有两个用户A和B.当你的服务器上有用户A和B,那时你可以为每个用户build立两个通道,根据他们的用户ID和名字以及一些dynamic号码的组合。 这为所有用户提供了默认频道来收听和订阅。 这些通道可用于将消息从服​​务器发送到客户端,这可以作为通知给客户端。

现在为了通信目的,可以有所有用户都订购的OpenTalks之类的频道,可以用于聊天。

您可以细化一对一对话的渠道制作。

 bayeux.getClient().subscribe('/'+channel_name, message); bayeux.getClient().publish('/'+channel_name, message); 

或者你可以使用

 const faye = require('faye'); var client = new faye.Client('http://localhost:3000/faye',{timeout: 20}); client.connect(); client.subscribe('/'channel_name, function(message){console.log(message);}); client.publish ('/'+response1[0].channel_id, {channel_name: channel_name,message: message}); 
Interesting Posts