2016-10-20 38 views
4

我接工作的很好,當客戶端有令牌(由Laravel JWT提供的),但是,需要與客戶打交道,而他們還沒有通過身份驗證,我想類似於:如何發出和收到whitout一個身份驗證使用socketio,智威湯遜

io.sockets 
.on('connection', socketioJwt.authorize({ 
    secret: process.env.JWT_SECRET, 
    timeout: 15000 // 15 seconds to send the authentication message 
})) 

.on('authenticated', function(socket) { 
    console.log('Authenticated!!'); 
    // This works: 
    socket.on('setDataRegistered', function(datos) { 
    console.log('Registering!'); 
    }); 
}) 
// This doesn't works: 
.on('config', function(socket) { 
    console.log('A client wants to be configured before authenticated!'); 
}) 

如何在通過身份驗證之前從FrontEnd調用'config'(socket.emit('config'))?

感謝您的幫助。對不起我的英文。

+0

Helloooo,有人可以幫我請。 – bluesky777

回答

1

我做的是這樣的:

io.on('connection', function (socket) { 
     // Client connected but not authenticated. Client has to emit to 'authenticate' for authentication 

     socket.on('authenticate', function (data, fn) { 
      // Authenticate socket. 

      // define more events 
     }); 

     // still accessible to unauthorised sockets 
     socket.on("other-event", function(data) { 

     }); 
}); 

我不使用任何中間件進行身份驗證。但那只是我。我從來沒有找到它的用途。

+0

我在想那個。但是,我如何驗證'authenticate'函數中的jwt套接字?這就是中間件爲我做的。那麼,我該如何手動進行呢?謝謝! – bluesky777

+0

我使用Laravel來驗證並創建令牌。稍後,我將該令牌發送給nodejs。 – bluesky777

+0

@ bluesky777您只需解碼令牌。檢查你的圖書館的文檔。 –