没有为websocket调用环回身份验证方法

时间:2018-07-15 09:05:26

标签: loopbackjs loopback

我正在使用环回进行websockets身份验证。这是我的代码:

server.js

app.io = require("socket.io")(app.start());
    require("socketio-auth")(app.io, {
        authenticate: function(socket, value, callback) {
            console.log(value);
            var AccessToken = app.models.CustomAccessToken;
            //get credentials sent by the client
            var token = AccessToken.find(
                {
                    where: {
                        and: [{ userId: value.userId }, { id: value.id }]
                    }
                },
                function(err, tokenDetail) {
                    if (err) throw err;
                    if (tokenDetail.length) {
                        callback(null, true);
                    } else {
                        callback(null, false);
                    }
                }
            ); //find function..
        } //authenticate function..
    });

    app.io.on("connection", function(socket) {
        console.log("a user connected");
        socket.on("disconnect", function() {
            console.log("user disconnected");
        });


    });

这是我的忠实客户

this.socket = io.connect("http://localhost:3000");
            const id = localStorage.getItem("id_token");
            const userId = localStorage.getItem("user_id");
            const self = this;
            this.socket.on('connect', function(){

                self.socket.emit('authentication', {id: id, userId: userId });
                self.socket.on('authenticated', function() {

                    console.log('User is authenticated');
                });
            });

在服务器控制台中,我只有2个输出:

server.js:93
a user connected
server.js:91
user disconnected

用户已连接并立即断开连接。我看不到身份验证功能的值,因为我看不到 console.log(value);。我做错什么了吗?

0 个答案:

没有答案