如何在 IOWebSocketChannel 中设置用户代理?

How can I set user-agent in IOWebSocketChannel?

我在 IOWebSocketChannel 工作,但我无法访问其中的 Webscoket 为了改变user-agent。我怎样才能做到这一点并为 IOWebSocketChannel 设置 user agent?

根据这个问题:Add option to set User Agent for Websocket #32008 和文档:WebSocket

您不能为 IOWebSocketChannel 设置用户代理,但您可以通过调用为所有 Websocket 实例设置 userAgent:

Websocket.userAgent = 'your desired agent';

这适用于所有 Websocket 实例,因此无需访问传递给 IOWebSocketChannel 的 websocket 实例对象。

如果您使用 IOWebSocketChannel.connect you can just add the user-agent in the headers parameter, or otherwise if you are upgrading to a IOWebSocketChannel from a WebSocket (with WebSocket.connect) 创建套接字) 在那里设置您的 headers。