1

我尝试通过我的 socketio v4 nativescript - nodejs 对我的用户进行身份验证

设置

"@angular/core": "~8.2.0",
"@triniwiz/nativescript-socketio": "^5.0.0",
"nativescript-angular": "~8.21.0",

客户端

我写了这个 app.module.ts

SocketIOModule.forRoot('http://10.0.2.2:3000', {
  debug: true,
  log: true,
  extraHeaders: {
    'authorization': `Bearer ${getString('ACCESS_TOKEN')}`
  },
}),

而且很简单this.socketIO.connect();

问题

问题是我的服务器根本没有收到我的额外标题。我无法正确验证。

我的研究

使用 Chrome 开发工具调试,我的 websocket 请求根本没有记录。 Chrome 开发工具

通过console.log('url', args);在 index.android.js (nativescript-socketio lib) 的第 73 行添加。上面this.socket = io.socket.client.IO.socket(args[0], opts.build()); 的标题正确存在

通过检查服务器接收到的内容。

{
  accept: '*/*',
  host: '10.0.2.2:3000',
  connection: 'Keep-Alive',
  'accept-encoding': 'gzip',
  'user-agent': 'okhttp/3.12.12'
}

使用其他工具:SocketIO Online Client Tool 服务器正确接收标头

{
  host: 'localhost:3000',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',     
  accept: '*/*',
  'accept-language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
  'accept-encoding': 'gzip, deflate',
  authorization: 'Bearer eyJhbGciOiJIU............V4MKGQ',
  origin: 'https://amritb.github.io',
  connection: 'keep-alive',
  'sec-gpc': '1'
}

最后,我认为这是一个 CORS 问题(我认为这不太可能,因为没有错误)。我的服务器套接字使用以下选项。如文档中所述

{
  cors: {
      origin: '*',
      allowedHeaders: ['authorization']
    }
  }
}

我发现了类似的问题,但他们的解决方案并没有改变任何东西。

有没有人遇到过这个问题?你还有其他建议吗?我没主意了。

感谢您的帮助

4

1 回答 1

-1

如果它只影响 Authorization 标头,您是否尝试过?

你可以尝试使用withCredentials=true

于 2021-05-25T19:45:57.490 回答