当前,我正在使用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"
错误。可能是因为我没有像动作电缆那样添加原点和通道详细信息。但是我不知道如何在这里添加它。任何帮助表示赞赏。