0

我正在使用 faye 消息系统,我想添加身份验证!我浏览了网站并按照教程进行操作。

在客户端上,我有一个传出消息的扩展:

var droneFaye = new faye.Client("/faye", {
            timeout: 120
        });
var USER_TOKEN = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs';

    droneFaye.addExtension({
        outgoing: function(message, callback) {
            if (message.channel !== '/meta/subscribe')
                return callback(message);

            message.ext = message.ext || {};
            message.ext.token = USER_TOKEN;
            console.log(message);
            callback(message);
        }
    });

在服务器上:

var token = 'ifd63cylqwsyaq9c2ptzywjujgtfpxs'

    var serverAuth = {
    incoming: function(message, callback) {
        // Let non-subscribe messages through

        if (message.channel !== '/meta/subscribe')
            return callback(message);

        // Get subscribed channel and auth token
        var msgToken = message.ext && message.ext.token;

        // Find the right token for the channel
        if (token !== msgToken) {
            message.error = 'Invalid subscription auth token';
        }
        console.log(message);
        callback(message);
    }
};

var adapter = new faye.NodeAdapter({
    mount:'/faye',
    timeout:45
});
adapter.addExtension(serverAuth);
adapter.attach(httpServer);

我在服务器上有这样的订阅:

adapter.getClient().subscribe("/drone/move", function(cmd) {});

好的!因此,当我启动服务器并且没有客户端时,它已经调用了订阅的扩展,它将在控制台上输出:

{ channel: '/meta/subscribe',
  clientId: '2isdeb0ds77zl0lh82ob1kqu29y1cajnv80',
  subscription: '/drone/move',
  id: '2',
  error: 'Invalid subscription auth token' }

一旦客户端连接到服务器,它将再次调用扩展并输出:

{ channel: '/meta/subscribe',
  clientId: '3kechs0c7smpc05z5o7d0a8qcd301d8zi41',
  subscription: '/drone/move',
  id: '3',
  ext: { userId: 18787, token: 'ifd63cylqwsyaq9c2ptzywjujgtfpxs' } }

所以这看起来不错!但是即使它们具有正确的令牌并且没有错误消息,也不会有其他消息到达服务器!

仅供参考。如果您向消息对象添加带有值的错误键,它不会将消息传递给它的订阅......它应该是这样的!..

此外,当我在扩展程序中注释掉 message.error 时,它工作正常,但当然没有身份验证。

那么有谁知道为什么即使没有客户端,服务器也会调用扩展程序,其次为什么即使消息对象中没有错误,faye 也不会将消息提供给它的订阅?

谢谢!

4

1 回答 1

0

还提出了问题,并在这里回答

这是由对 的调用引起的adapter.getClient().subscribe()

[...] 服务器端客户端 ( <adapter>.getClient()) 返回到服务器的连接,因此与客户端执行相同的扩展(传入、传出)。

[...] 附加一个传出扩展,您会看到它正在响应服务器客户端。

于 2013-07-11T04:16:22.790 回答