繁体   English   中英

socketio-jwt:连接到SocketIO,进行身份验证

[英]socketio-jwt: Connected to SocketIO, Authenticating

我遵循了多个教程来设置socketio-jwt ,但是每次看来我都没有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);
});

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

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

来自: dotenv github

而是将存储在../.env的文件中的变量../.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
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM