sch*_*mru 6 websocket ios swift
我正在尝试使用链接ws://link.net:8888/ws?token =创建与Web套接字的连接,以打开与服务器的通信.
我发现我可以使用Stream,但我不知道如何将access_token添加到最后.
Stream.getStreamsToHost(withName: host, port: port, inputStream: &inputStream, outputStream: &outputStream)
if inputStream != nil && outputStream != nil {
// Set delegate
inputStream!.delegate = self
outputStream!.delegate = self
// Schedule
inputStream!.schedule(in: .main, forMode: RunLoopMode.defaultRunLoopMode)
outputStream!.schedule(in: .main, forMode: RunLoopMode.defaultRunLoopMode)
print("Start open()")
// Open!
inputStream!.open()
outputStream!.open()
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,即使我尝试连接到没有访问密钥的链接,我也不成功.那么有没有人有办法如何打开连接到添加access_key的套接字我会非常感激?
我也尝试过https://github.com/swiftsocket/SwiftSocket,但它也没有选项可以添加任何东西,我现在唯一能尝试的是https://github.com/socketio/socket. io-client-swift.
EDIT1:所以在Dan Karbayev的评论之后我用代码检查了Starscream
let url = "ws://link.net:5000/ws?token=token"
let socket = WebSocket(url: URL(string: url)!)
socket.onConnect = {
print("websocket is connected")
}
//websocketDidDisconnect
socket.onDisconnect = { (error: Error?) in
print("websocket is disconnected: \
(error?.localizedDescription)")
}
//websocketDidReceiveMessage
socket.onText = { (text: String) in
print("got some text: \(text)")
}
//websocketDidReceiveData
socket.onData = { (data: Data) in
print("got some data: \(data.count)")
}
//you could do onPong as well.
socket.connect()
Run Code Online (Sandbox Code Playgroud)
但由于一些奇怪的原因,套接字方法都没有唤醒.
编辑2:
最后我用https://github.com/tidwall/SwiftWebSocket结束了.它马上工作了.
但感谢所有人的帮助.
| 归档时间: |
|
| 查看次数: |
6800 次 |
| 最近记录: |