简体   繁体   English

Java / Android Websocket无法连接到Spark Websocket服务,但JavaScript可以

[英]Java/Android websocket cannot connect to Spark websocket service but JavaScript can

I want to connect to an open Spark websocket server. 我想连接到打开的Spark websocket服务器。 For me, this server is located at ws://192.168.56.1:4567/chat/. 对于我来说,该服务器位于ws://192.168.56.1:4567 / chat /。 When I try connecting to it using JavaScript running on the computer, it works: 当我尝试使用计算机上运行的JavaScript连接到它时,它可以工作:

var websocket = new WebSocket("ws://192.168.56.1:4567/chat/");

This code successfully connects to the websocket. 此代码成功连接到websocket。 On Android using java-websocket, this snippet looks like this: 在使用java-websocket的Android上,此代码段如下所示:

val client = Client(URI("ws://192.168.56.1:4567/chat/"))

I use Kotlin, the equivalent Java Code is 我使用Kotlin,等效的Java代码是

Client client = new Client(new URI("ws://192.168.56.1:4567/chat/"));

where Client is a class extending WebSocketClient. 其中Client是扩展WebSocketClient的类。 When I try to connect to the websocket, it simply does not work and times out as if this websocket didn't exist at all. 当我尝试连接到websocket时,它根本不起作用,并且超时,好像此websocket根本不存在。 I do have INTERNET enabled in my manifest file: 我的清单文件中确实启用了INTERNET:

<uses-permission android:name="android.permission.INTERNET" />
<application
[...]

and I tried to ping Google and that worked, so I really don't know what causes this. 而且我尝试对Google进行ping操作,但这种方法确实有效,所以我真的不知道是什么原因造成的。 Thank you for any help! 感谢您的任何帮助!

So for some reason you have to use Draft_17 like this: 因此由于某种原因,您必须像这样使用Draft_17:

val client = Client(URI("ws://192.168.56.1:4567/chat/"), Draft_17())

This is not well documented. 这没有很好的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM