使用React Native WebSocket发送Cookie [英] Sending cookies with react native websockets

查看:233
本文介绍了使用React Native WebSocket发送Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用本机 websockets ,但是不能弄清楚我如何在websocket中包含cookie,有什么建议吗?

So I'm using react native websockets but cannot figure out how i can include cookies in websockets, any suggestions?

推荐答案

目前尚无自动方法。 WebSocket构造函数有第三个参数(未记录),用于将自定义HTTP标头传递给连接请求。

At the moment there is no automatic way to do it. There is a third (undocumented) parameter for the WebSocket constructor to pass custom HTTP headers to the connect request.

WebSocket(url, '', {Cookie: 'key=value'});

此功能可在iOS上使用,但我尚未在Android上进行过测试,但WebSocket实现看起来像

This works on iOS, I haven't tested it on Android but the WebSocket implementation looks like it is supported there as well.

如果您只需要传递会话ID或auth令牌,则将其作为url中的GET参数传递可能会更容易。在快速变化的框架中依赖未记录的行为可能很危险。

If you just need to pass a session id or auth token, it's probably easier to pass it as a GET param in the url. Relying on undocumented behavior in a rapidly changing framework can be dangerous.

这篇关于使用React Native WebSocket发送Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆