Xcode Swift项目连接WebSocket遇NSURLErrorDomain Code=-1005错误求助
iOS WebSocket连接报错NSURLErrorDomain Code=-1005(网络连接丢失)求助
我正在开发iOS项目,需要连接WebSocket,以下是实现连接的代码:
class WebSocketTaskConnection: NSObject, WebSocketConnection, URLSessionWebSocketDelegate { var delegate: WebSocketConnectionDelegate? var webSocketTask: URLSessionWebSocketTask! var urlSession: URLSession! let delegateQueue = OperationQueue() init(token: String) { super.init() let tokenFilter = token.substring(from: 7) var components = URLComponents(string: WebSocketUrls.baseSocketUrl)! components.queryItems = [URLQueryItem(name: "token", value: tokenFilter), URLQueryItem(name: "os_type", value: "ios")] print("WebSocketTaskConnection URL", components.url ?? "---") urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: delegateQueue) webSocketTask = urlSession.webSocketTask(with: components.url!) } func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) { self.delegate?.onConnected(connection: self) } func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) { self.delegate?.onDisconnected(connection: self, error: nil) } func connect() { webSocketTask.resume() listen() } func disconnect() { webSocketTask.cancel(with: .goingAway, reason: nil) } func listen() { webSocketTask.receive { result in switch result { case .failure(let error): self.delegate?.onError(connection: self, error: error) case .success(let message): switch message { case .string(let text): self.delegate?.onMessage(connection: self, text: text) case .data(let data): self.delegate?.onMessage(connection: self, data: data) @unknown default: fatalError() } self.listen() } } } func send(text: String) { webSocketTask.send(URLSessionWebSocketTask.Message.string(text)) { error in if let error = error { self.delegate?.onError(connection: self, error: error) } } } func send(data: Data) { webSocketTask.send(URLSessionWebSocketTask.Message.data(data)) { error in if let error = error { self.delegate?.onError(connection: self, error: error) } } } }
尝试连接时出现如下错误:
SOCKET >> Connection error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://chat.wifiattendance.com:3015/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxMDk1NSIsInVuaXF1ZV9uYW1lIjoiZmFyZXlhLmtAY2xvdWR4cGVydGUuY29tIiwiRW1wbG95ZWVJZCI6IjUwNTMiLCJPcmdJZCI6IjEiLCJMb2NhdGlvbklkIjoiODg3IiwiTG9jYXRpb25OYW1lIjoiQXVyYW5nYWJhZCIsIlJvbGVJZCI6IjIxNCIsIlJvbGVOYW1lIjoiRW1wbG95ZWUiLCJuYmYiOjE3MDU1NjA1MjksImV4cCI6MTcwNTgxOTcyOSwiaWF0IjoxNzA1NTYwNTI5fQ.PC-ha0pOQkzKVL-ePCVEfocSMT-TyFxZ_npGOJVfRyQ&os_type=ios, NSLocalizedDescription=The network connection was lost., NSErrorFailingURLKey=https://chat.wifiattendance.com:3015/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxMDk1NSIsInVuaXF1ZV9uYW1lIjoiZmFyZXlhLmtAY2xvdWR4cGVydGUuY29tIiwiRW1wbG95ZWVJZCI6IjUwNTMiLCJPcmdJZCI6IjEiLCJMb2NhdGlvbklkIjoiODg3IiwiTG9jYXRpb25OYW1lIjoiQXVyYW5nYWJhZCIsIlJvbGVJZCI6IjIxNCIsIlJvbGVOYW1lIjoiRW1wbG95ZWUiLCJuYmYiOjE3MDU1NjA1MjksImV4cCI6MTcwNTgxOTcyOSwiaWF0IjoxNzA1NTYwNTI5fQ.PC-ha0pOQkzKVL-ePCVEfocSMT-TyFxZ_npGOJVfRyQ&os_type=ios}
已尝试的操作:
- 重置并重启模拟器
- 重启Xcode
- 在iPhone真机运行并重装应用
恳请大家提供解决思路。
内容的提问来源于stack exchange,提问作者Fareya Nibras
相关产品推荐
相关产品推荐

