Websocket 不断断开连接(红蜘蛛) [英] Websocket keeps disconnecting (Starscream)

查看:62
本文介绍了Websocket 不断断开连接(红蜘蛛)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试订阅本地网络中 Mac 上托管的 Websocket.但它在连接后几乎立即断开连接.我可以发送消息,我可以在我的 Mac 上看到它正在接收它们(至少是命令),但随后它立即断开连接.

I'm trying to subscribe to a Websocket hosted on a Mac in my local network. but it disconnects almost immediately after connecting. I can send messages and I can see on my Mac it is receiving them (at least commands) but then it disconnects immediately.

它应该是这样工作的:我连接我发送一个json格式的字符串:{"+":["v.altitude"]}(订阅海拔)Websocket 定期响应 {"v.altitude":71.323}

It is supposed to work like this: I connect I send a json formatted string : {"+":["v.altitude"]} (to subscribe to the altitude) The Websocket responds regularly with {"v.altitude":71.323}

我在我的 Mac 上使用 websocoat 在本地尝试了它,它看起来像这样:[Image] (https://ibb.co/JqjFSs7)

I tried it locally on my Mac with websocoat and it looks like this: [Image] (https://ibb.co/JqjFSs7)

Xcode 控制台输出:

The Xcode console outputs this:

正在连接.
已连接.
DISCONNECTED 可选(操作无法完成(Starscream.WSError 错误 1.)").

Connecting.
CONNECTED.
DISCONNECTED Optional("The operation couldn’t be completed (Starscream.WSError error 1.)").

import UIKit
import Starscream

class ViewController: UIViewController, WebSocketDelegate, WebSocketPongDelegate {

    func websocketDidReceivePong(socket: WebSocketClient, data: Data?) {
        print("PONG")
    }

    var websocket: WebSocket = WebSocket(url: URL(string: "ws://192.168.178.23:8085/datalink")!)
    let jsonObject: [String: Any] = ["run":["f.light"]]//["+":["v.altitude", "v.lightValue", "v.gearValue"]]
    let dic: NSDictionary = ["run" : ["f.light"]]
    var components = URLComponents()

    override func viewDidLoad() {
        components.scheme = "ws"
        components.host = "192.168.178.23"
        components.path = "/datalink"
        components.port = 8085
        let url = components.url
        super.viewDidLoad()
        print(url!)
        websocket = WebSocket(url: url!)
        websocket.delegate = self
        print("Connecting")
        websocket.connect()
    }

    func websocketDidConnect(socket: WebSocketClient) {
        print("CONNECTED")

        let messageString = "{\"run\":[\"f.light\"]}"

        websocket.write(string: messageString)
    }

    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
        print("DISCONNECTED \(error?.localizedDescription)")
    }

    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
        print("MESSAGE: STRING")
    }

    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
        print("MESSAGE: DATA")
    }
}

推荐答案

红蜘蛛 4 的解决方案是

For Starscream 4 the solution is

socket = WebSocket(request: request, certPinner: FoundationSecurity(allowSelfSigned: true))

这篇关于Websocket 不断断开连接(红蜘蛛)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆