jquery - 如何创建要订阅的 View 的路径,由 bayeux.getClient().publish(

标签 jquery node.js faye bayeux

我正在使用 Node js 和 faye 来简单地将一些消息传递给客户端,

我创建一个 Node 服务器

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

问题是,我不知道如何创建 channel

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

如何订阅,请帮忙。提前致谢......

最佳答案

基本上,在服务器端,您可以创建一个逻辑来创建不同的 channel ,并将其保存在数据库中,以便您的客户端订阅它并使用它进行通信。

例如,可能有两个用户,A 和 B。 当用户 A 和 B 登录到您的服务器上时,您可以根据用户 ID、名称以及一些动态数字的组合,为每个用户创建两个 channel 。这为所有用户提供了默认的收听和订阅 channel 。这些 channel 可用于从服务器向客户端发送消息,这些消息可以充当向客户端的通知。

现在出于通信目的,可以有一个 channel ,例如OpenTalks,所有用户都订阅该 channel ,可以用于聊天。

您可以针对一对一对话进一步完善 channel 制作。

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

关于jquery - 如何创建要订阅的 View 的路径,由 bayeux.getClient().publish(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10674454/

相关文章:

jQuery 如何比较两个 CSS 值

jquery - 如何选择列表中特定类(class)的每个最后一个 child

javascript - Node js 中的时分和秒的日期时间差异 moment.js

node.js - 使用 NodeJS 和 ObjectionJs 进行分页查询

javascript - 如何在 node.js 中关闭 firebase 连接

ruby-on-rails - 如何在 rails 中使用 PrivatePub 和 Faye 查找当前订阅的数量

javascript - 在第 3 方网站上运行的安全 JavaScript

jquery - 使用同位素过滤和 InfiniteScroll 加载重复页面/内容

android - 隐藏滚动条 Android KitKat 4.4

ruby-on-rails - 在Rails应用中进行长轮询