我有一个为我正在创建的应用程序设置的Java服务器。 Java服务器在尝试连接时需要新客户端:无法将Starscream WebSocket连接到Java ServerSocket
//Continuously accept new user clients
try(ServerSocket serverSocket = new ServerSocket(portNumber)){
while(!Thread.currentThread().isInterrupted()){
//I do some stuff here
...
//Then accept the socket
Socket s = serverSocket.accept();
//Then I do stuff with s; the user is connected
...
}
} catch (IOException e) {
System.err.println("Could not listen on port "+portNumber);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
在Android上,我只是使用Socket类,而且我没有问题。它连接到在AWS上运行在EC2上的Java服务器的端点,我没有问题。但是,从iOS发现的情况来看,第三方库很受推荐,我已经决定使用Starscream(目前)。
我不能得到简单的连接示例工作(在Starscream github页上的那个)。有一些教程如this用Node.js设置了一个本地服务器,但我宁愿不加入,因为我已经有了一个“非常简单的服务器”。
这里是我的SWIFT代码:
class ViewVontroller: UIViewController, WebSocketDelegate{
var socket = WebSocket(url: URL(string: "ec2-12-345-678-910.compute-1.amazonaws.com/:4922/")!)
override func viewDidLoad(){
super.viewDidLoad()
socket.delegate = self
print("connecting")
socket.connect()
print("should've connected")
}
... //The rest of the protocol is implemented below with simple print statements as the body
它输出:
connecting
should've connected
[timestamp/project name...] [] nw_connection_get_connected_socket_block_invoke 1 Connection has no connected handler
Websocket disconnected: The operation couldn't be completed. Operation timed out
是否有某种问题与红蜘蛛的WebSockets连接到Java的ServerSocket?我已经阅读了一些在别人的案例中提出这些问题的东西,但我几乎不知道套接字的底层实现。
我刚刚开始浏览您的链接,但这是我寻找的路径。谢谢! – Xenalin