首页 \ 问答 \ 在React-Native上升级连接(Connection Upgrade on React-Native)

在React-Native上升级连接(Connection Upgrade on React-Native)

所以我目前正在使用React-Native开发一个应用程序,并且我必须实现Websocket。 现在我正在尝试将我的应用程序连接到Mattermost。 对于ws,我们必须从html获取请求开始,这将被升级为ws请求。 示例:

URL: https://URL/api/v3/users/websocket
Méthod : GET
Adress : 10.10.10.10:8080
Code d’état : 200

Response (39 o) 
Server  "nginx"
Date    "Tue, 08 Aug 2017 12:33:21 GMT"
Connection  "upgrade"
Upgrade "websocket"
Sec-WebSocket-Accept    "vG2CYVx3/p+6ei/8e5fGMN8qq7A="

Request's header (769 o)    
Host    "URL"
User-Agent  "Mozilla/5.0 (Windows NT 6.1; … Gecko/20100101 Firefox/54.0"
Accept  "text/html,application/xhtml+x…lication/xml;q=0.9,*/*;q=0.8"
Accept-Language "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
Accept-Encoding "gzip, deflate, br"
Sec-WebSocket-Version   "13"
Origin  "URL"
Sec-WebSocket-Extensions    "permessage-deflate"
Sec-WebSocket-Key   "HsX9INf0fr7x0cQAglMyjg=="
Cookie  "MMAUTHTOKEN=7xaaaaaaaaahezhatnwxwih;"
Connection  "keep-alive, Upgrade"
Upgrade "websocket"

所以我的问题是如何做一个可升级的连接反应,并用它来建立一个wss连接。

目前我正在使用axios为我的websocket连接创建我的html请求和socket.io。


So i'm currently develloping an app using React-Native and i have to implement Websocket. Now i'm trying to connect my app to Mattermost. For ws, we have to begin with an html get request, that will be upgraded to a ws request. Example :

URL: https://URL/api/v3/users/websocket
Méthod : GET
Adress : 10.10.10.10:8080
Code d’état : 200

Response (39 o) 
Server  "nginx"
Date    "Tue, 08 Aug 2017 12:33:21 GMT"
Connection  "upgrade"
Upgrade "websocket"
Sec-WebSocket-Accept    "vG2CYVx3/p+6ei/8e5fGMN8qq7A="

Request's header (769 o)    
Host    "URL"
User-Agent  "Mozilla/5.0 (Windows NT 6.1; … Gecko/20100101 Firefox/54.0"
Accept  "text/html,application/xhtml+x…lication/xml;q=0.9,*/*;q=0.8"
Accept-Language "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"
Accept-Encoding "gzip, deflate, br"
Sec-WebSocket-Version   "13"
Origin  "URL"
Sec-WebSocket-Extensions    "permessage-deflate"
Sec-WebSocket-Key   "HsX9INf0fr7x0cQAglMyjg=="
Cookie  "MMAUTHTOKEN=7xaaaaaaaaahezhatnwxwih;"
Connection  "keep-alive, Upgrade"
Upgrade "websocket"

So my question is how to do an upgradable connexion on react, and use it to make a wss connection.

For the moment i was using axios to make my html requests and socket.io for my websocket connection.


原文:https://stackoverflow.com/questions/45567841
更新时间:2022-09-15 17:09

最满意答案

由于类型擦除,你不能。 请参阅: 为通用接口获取错误:接口Observer不能多次使用不同的参数实现:

我会做的是使用一个delgate并有你的CalculatorAB,但它不会实现ICalculator,或者更好的是,让CalculatorAB实现ICalcuator。 然后,您的方法可以检查实例并转发到适当的方法。

public CalculatorAB implements ICalculator<CalculableObject> {
  void calculate(CalculableObject obj){
    if(obj instanceof TypeA)
      calculate((TypeA)obj);
    else if(obj instanceof TypeB)
      calculate((TypeB)obj);
  }
  void calculate(TypeA obj){...}
  void calculate(TypeB obj){...}
}

Because of type erasure, you can't. See: Getting error for generic interface: The interface Observer cannot be implemented more than once with different arguments:

What I would do is use a delgate and have your CalculatorAB but it simply doesn't implement ICalculator, or better yet, have CalculatorAB implements ICalcuator. Your method can then check instance of and forward to the appropriate method.

public CalculatorAB implements ICalculator<CalculableObject> {
  void calculate(CalculableObject obj){
    if(obj instanceof TypeA)
      calculate((TypeA)obj);
    else if(obj instanceof TypeB)
      calculate((TypeB)obj);
  }
  void calculate(TypeA obj){...}
  void calculate(TypeB obj){...}
}

相关问答

更多
  • 我在这里复制了部分答案到另一个问题 ,解释了对所谓“自我类型”的渴望以及Java中的解决方法。 方法链接 而不是写作 foo.doA(); foo.doB(); 很多人宁愿写 foo.doA().doB(); 不幸的是,这种语言并不直接支持方法链接,尽管它正在成为一个越来越需要的功能。 解决方法是使doA()返回foo 。 这有点脏但可以接受。 但是,如果foo处于类型层次结构中,则解决方法已被破坏 class Bar Bar doA() class Foo extends Bar F ...
  • Vector类实现了一个可增长的对象数组,我认为你正在寻找map, java.util.Map ,map的实现是HashMap Map map = new HashMap(); map.put(1,"str"); The Vector class implements a growable array of objects, I think you are looking for map, java.util. ...
  • 这个答案你可以在这里找到: http : //golang.org/doc/faq#generics 为什么Go没有通用类型? 在某些时候可能会添加泛型。 我们对他们并不感到紧迫,尽管我们了解一些程序员的意图。 通用型是方便的,但它们在系统和运行时的复杂性方面都是昂贵的。 我们还没有发现一个设计,赋予与复杂性相称的价值,尽管我们继续考虑。 同时,Go的内置地图和切片,以及使用空接口构建容器(具有明确的取消装箱)的能力意味着在许多情况下,如果不太顺利,可以编写执行泛型启动的代码。 这仍然是一个公开的问题。 t ...
  • Java中的泛型是一个完全编译时的构造 - 编译器将所有通用用法转换为正确的类型。 这是为了保持与以前的JVM运行时的向后兼容性。 这个: List list = new ArrayList(); list.add(new ClassA()); ClassA a = list.get(0); 变成(大致): List list = new ArrayList(); list.add(new ClassA()); ClassA a = (ClassA)list.get(0); ...
  • 由于类型擦除,你不能。 请参阅: 为通用接口获取错误:接口Observer不能多次使用不同的参数实现: 我会做的是使用一个delgate并有你的CalculatorAB,但它不会实现ICalculator,或者更好的是,让CalculatorAB实现ICalcuator。 然后,您的方法可以检查实例并转发到适当的方法。 public CalculatorAB implements ICalculator { void calculate(CalculableObjec ...
  • 你不应该(也不能)为此使用泛型。 相反,只需提供两种重载方法即可: public void myMethod(String value); // and public void myMethod(Provider value); 由于无论如何都需要进行一些不同的处理,所以这种方式实际上更简单。 You should not (and can not) use generics for this. Instead simply provide two overloaded method: public vo ...
  • 现在我的问题是:Java是否需要支持才能在本地定义类型? 否.type-parameter的最小范围是方法,即为了使for循环具有类型T ,您必须将封闭方法定义为泛型或封闭类。 例如: void method(List myClasses) { for (final SomeClass myClass : myClasses) { myClass.doSomething(myRetriever.get(myClass)); } } N ...
  • 在使用之前,您需要声明该方法的type参数。 要创建泛型方法,请在返回类型之前声明type参数: private void assertIteratorThrowsNoSuchElement(Deque deque, Class cl) { } 此外,我没有看到任何使用第二参数。 你甚至没有使用它。 类型参数T是从您传递的实际类型中自动推断出来的。 如果您已为此目的添加它,则可以将其删除。 只需保留第1个参数。 You need to declare the type paramete ...
  • X不能是A,因为左侧的泛型中的内容必须与右侧的泛型相匹配。 但是,通过说X是“?extends A”,它告诉java g的泛型类型是至少具有A的功能的任何类。 简单地说,G!= G,但G可以保持G,G或任何其他延伸A. G意味着通用必须是A. X can't be A because what goes in the generics on the left side must match the generics on the right side. However, by saying X is "? ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)