如何在 Flutter WebSocketChannel 中传递 Authentication Header?

Has*_*tty 9 websocket dart flutter

我正在使用 Flutter web_socket_channel包与服务器通信。该类WebSocketChannel不采用标头参数。

factory WebSocketChannel.connect(Uri uri, {Iterable<String>? protocols}) =>
      platform.connect(uri, protocols: protocols);
Run Code Online (Sandbox Code Playgroud)

当使用IOWebSocketChannel传递标头时,我会像此处Unsupported operation: Platform._version给出的那样进入网络。它在 Android 和 iOS 上运行良好。

pow*_*rus 1

简洁的答案是:不可以,因为只能设置您的 URI 和协议字段中的路径

更长的答案:在纯 JS WebSocket API中,没有方法指定浏览器发送的额外标头。

您只能指定

GET query(was://mysockets.com/path)
Run Code Online (Sandbox Code Playgroud)

和头协议称为Sec-WebSocket-Protocol

结论:如果您无法在本机代码中实现此类功能,那么您的 flutter web 应用程序将无法使用此功能

快乐编码