SocketIOClient:操作无法完成。 (Starscream.WSError错误1。)

时间:2018-06-21 07:28:10

标签: ios swift ssl websocket socket.io

有关Starscream.WSError错误1的问题。我一直在尝试使用SocketIOClient连接到Websocket。但是出现Starscream.WSError错误1.用于连接Socket的代码为

import UIKit
import SocketIO

class SocketIOManager: NSObject, URLSessionDelegate {

    static var shared = SocketIOManager()
    let manager = SocketManager(socketURL: URL(string: "https:sample.com")!, config: [.log(true), .compress]) //https url
    var socket: SocketIOClient!

    override init() {
        super.init()
        socket = manager.defaultSocket
    }

    func connectSocket() {
        let token = "hhhhhhh" //token
        let specs: SocketIOClientConfiguration = [
            .connectParams(["access_token": token]),
            .log(true),
            .forceNew(true),
            .selfSigned(true),
            .secure(true),
            .reconnects(true),
            .forceWebsockets(true),
            .reconnectAttempts(3),
            .reconnectWait(3),
            .sessionDelegate(self),
            ]
        self.manager.config = specs
        socket.on(clientEvent: .connect) {data, ack in
            print("socket connected")
            self.socket.emitWithAck("emit", with: []).timingOut(after: 2, callback: { (data) in
                print(data)
            })
        }

        socket.on("listen") { (dataArray, ack) in
            print(dataArray)
        }

        socket.on(clientEvent: .error) {data, ack in
            print(data)
        }

        socket.on(clientEvent: .disconnect) {data, ack in
            print(data)
        }

        socket.onAny { (data) in
            // print(data)
        }

        socket.connect()

    }

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    }

    func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
        if let err = error {
            print("Error: \(err.localizedDescription)")
        } else {
            print("Error. Giving up")
        }
    }

}

0 个答案:

没有答案