[英]how to get parameters from websocket client request
我正在嘗試設置授權參數ws://token:@url
如何從服務器中的此請求中獲取令牌參數。
我看了一下websocket
庫,這是我想到的:
// You need to disable `autoAcceptConnections`...
let wss = new WebSocketServer({ httpServer : server, autoAcceptConnections : false });
// ...so the `request` event is fired, which is needed to access the url parameters
wss.on('request', function(req) {
// Parse the requested URL:
let url = require('url').parse(req.httpRequest.url);
// Assume that the token is passed as path:
// ws://url/TOKEN
let token = url.pathname.substring(1); // .substring(1) to strip off the leading `/`
// Validate token (implementation-dependent):
if (! isValidToken(token)) return req.reject();
// Accept the request.
return req.accept();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.