gpt4 book ai didi

node.js - 如何从websocket客户端请求中获取参数

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:19 25 4
gpt4 key购买 nike

我正在尝试设置授权参数ws://token:@url如何从我的服务器中的此请求获取 token 参数。

最佳答案

我查看了 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();
});

关于node.js - 如何从websocket客户端请求中获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com