【问题标题】:Websocket connection via starscream in ios通过 ios 中的红蜘蛛进行 Websocket 连接
【发布时间】:2020-02-01 13:08:45
【问题描述】:

目前我正在使用Action Cable client 连接到 URL 并订阅频道。但图书馆似乎有一些问题,因为它偶尔无法订阅频道。以下是我当前的 Action cable 客户端设置代码

func setupActionCable(){

        guard let urlString = URL(string: "wss://mysocketurl.com/path") else {return}

        let headers = ["Origin":"https://mysocketurl.com"]
        self.client = ActionCableClient(url: urlString, headers:headers)

        self.client?.willConnect = {
            print("Will Connect")
        }

        self.client?.onConnected = {
            print("Connected to \(String(describing: self.client?.url))")
            print("Client = \(String(describing: self.client))")
            self.createChannel()
        }

        self.client?.onDisconnected = {(error: ConnectionError?) in
            print("Disconected with error: \(String(describing: error))")
        }

        self.client?.willReconnect = {
            print("Reconnecting to \(String(describing: self.client?.url))")
            return true
        }

        self.client?.onPing = {

            guard let channel = self.channel else {return}
            print("Ping received = \(String(describing: channel))")
        }
    }

    func createChannel(){

        let room_identifier = ["room_id" : roomID]
        self.channel = client?.create("MyChannel", identifier: room_identifier)

        self.channel?.onSubscribed = {
            print("Subscribed to \(self.ChannelIdentifier) with simulation Id = \(self.simulationID)")
        }

        self.channel?.onReceive = {(data: Any?, error: Error?) in

            print("****** channel response data = \(String(describing: data))")
            if let error = error {
                print(error.localizedDescription)
                return
            }
        }

        self.channel?.onUnsubscribed = {
            print("Unsubscribed")
        }

        self.channel?.onRejected = {
            print("Rejected")
        }
    }

现在我正在尝试迁移到红蜘蛛来解决这个问题。但我不太确定如何在下面设置它是我的启动代码。 func setupStarScream() {

        var request = URLRequest(url: URL(string: "wss://mysocketurl.com/path")!)
        request.httpMethod = "POST"
        request.timeoutInterval = 5
        socket = WebSocket(request: request)
        socket.delegate = self
        socket.connect()
    }

这总是给我一个"Invalid HTTP upgrade" 错误。可能是因为我没有像在动作电缆中那样添加来源和频道细节。但我不知道如何在这里添加它。任何帮助表示赞赏。

【问题讨论】:

    标签: ios sockets websocket swift4.2 starscream


    【解决方案1】:

    试试这个

    func setupStarScream(){
            var request = URLRequest(url: URL(string: "wss://mysocketurl.com/path")!)
            request.addValue("https://mysocketurl.com", forHTTPHeaderField: "Origin")
            socket = WebSocket(request: request)
            socket?.delegate = self
            socket.connect()
        }
    

    连接后创建这样的频道

    func websocketDidConnect(socket: WebSocketClient) {
            print("websocket is connected")
            createChannel()
        }
    
    func createChannel()
        {
            let strChannel = "{ \"channel\": \"MyChannel\",\"room_id\": \"\(roomID)\" }"
            let message = ["command" : "subscribe","identifier": strChannel]
    
            do {
                let data = try JSONSerialization.data(withJSONObject: message)
                if let dataString = String(data: data, encoding: .utf8){
                    self.socket?.write(string: dataString)
                }
    
            } catch {
                print("JSON serialization failed: ", error)
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2017-10-24
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      相关资源
      最近更新 更多