通过ios中的红蜘蛛连接Websocket
目前我正在使用连接到URL并订阅频道。但该图书馆似乎存在一些问题,因为它偶尔无法订阅该频道。下面是Action cable客户端的当前设置代码通过ios中的红蜘蛛连接Websocket,ios,sockets,websocket,swift4.2,starscream,Ios,Sockets,Websocket,Swift4.2,Starscream,目前我正在使用连接到URL并订阅频道。但该图书馆似乎存在一些问题,因为它偶尔无法订阅该频道。下面是Action cable客户端的当前设置代码 func setupActionCable(){ guard let urlString = URL(string: "wss://mysocketurl.com/path") else {return} let headers = ["Origin":"https://mysocketurl.com"]
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")
}
}
现在我正试图迁移到红蜘蛛来解决这个问题。但我不确定如何设置,下面是我的启动代码。
红蜘蛛(){
这总是给我一个“无效的HTTP升级”
错误。可能是因为我没有像在action cable中那样添加源站和频道详细信息。但我不知道如何在此处添加它。非常感谢您的帮助。试试这个
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)
}
}
试试这个
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)
}
}