【问题标题】:socketio-jwt: Connected to SocketIO, Authenticatingsocketio-jwt:连接到 SocketIO,正在认证
【发布时间】:2017-07-30 16:55:45
【问题描述】:

我已经按照多个教程设置socketio-jwt,但每次似乎我都没有通过这部分:

Connected to SocketIO, Authenticating

有什么想法吗?

客户端:

<h1>Socket Connection Status: <span id="connection"></span></h1>

<script type="text/javascript">
    $(document).ready(function () {
        socketIOConnectionUpdate('Requesting JWT Token from Laravel');

        $.ajax({
            url: 'http://localhost:8000/token?id=1'
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            socketIOConnectionUpdate('Something is wrong on ajax: ' + textStatus);
        })
        .done(function (result, textStatus, jqXHR) {

            socketIOConnectionUpdate('Connected to SocketIO, Authenticating')
            /* 
            make connection with localhost 3000
            */
            var token = result.token;
            var socket = io.connect('http://localhost:3000');
            socket.on('connect', function () {
              socket
                .emit('authenticate', {token: token}) //send the jwt
                .on('authenticated', function () {
                  console.log('authenticated');
                  socketIOConnectionUpdate('Authenticated');
                })
                .on('unauthorized', function(msg) {
                  socketIOConnectionUpdate('Unauthorized, error msg: ' + msg.message);
                  throw new Error(msg.data.type);
                })
            });
        });
    });

    /* 
    Function for print connection message
    */
    function socketIOConnectionUpdate(str) {
        $('#connection').html(str);
    }

服务器端

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketioJwt = require('socketio-jwt');
var dotenv = require('dotenv').config({path:'../.env'});

var port = 3000;

io
    .on('connection', socketioJwt.authorize({
        secret: dotenv.JWT_SECRET,
        timeout: 100 // 15 seconds to send the authentication message
    }))
    .on('authenticated', function(socket){
        console.log('connected & authenticated: ' + JSON.stringify(socket.decoded_token));
        socket.on('chat message', function(msg){
            debugger;
            io.emit('chat message', msg);
        });
    });

http.listen(port, function(){
  console.log('listening on *:' + port);
});

【问题讨论】:

    标签: javascript php node.js laravel socket.io


    【解决方案1】:

    您可能误解了dotenv 的工作原理,因为您尝试使用它的返回值。

    Dotenv 是一个零依赖模块,可将环境变量从 .env 文件加载到 process.env 中。

    发件人:dotenv github

    相反,它将存储在位于../.env 的文件中的变量导出为环境变量,作为process.env 的一部分提供。

    所以不要这样:

    var dotenv = require('dotenv').config({path:'../.env'});
    socketioJwt.authorize({
      secret: dotenv.JWT_SECRET,
      timeout: 100
    })
    

    这样做

    // do this near the entry point to your application!!
    require('dotenv').config({path:'../.env'});
    
    socketioJwt.authorize({
      secret: process.env.JWT_SECRET,
      timeout: 100
    })
    

    【讨论】:

    • 感谢您的回答。我仍然收到同样的错误。
    猜你喜欢
    • 2012-05-23
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多