ios – 红蜘蛛代表没有被召集

前端之家收集整理的这篇文章主要介绍了ios – 红蜘蛛代表没有被召集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
变量不是nil,我有一个很好的连接,url是正确的,但没有调用委托方法.我也正在实现WebSocketDelegate
let socket = WebSocket(url: NSURL(string: "UrlHere:port/")!)
            socket.delegate = self;
            socket.connect()
            if socket.isConnected {
                print("websocket is connected")
            }

    func websocketDidConnect(ws: WebSocket) {
        print("websocket is connected")
    }

    func websocketDidDisconnect(ws: WebSocket,error: NSError?) {
        if let e = error {
            print("websocket is disconnected: \(e.localizedDescription)")
        } else {
            print("websocket disconnected")
        }
    }

    func websocketDidReceiveMessage(ws: WebSocket,text: String) {
        print("Received text: \(text)")
    }

    func websocketDidReceiveData(ws: WebSocket,data: NSData) {
        print("Received data: \(data.length)")
    }

    func websocketDidReceivePong(socket: WebSocket) {
        print("Got pong!")
    }

解决方法

套接字应该是您的类的属性或变量,以确保它附近.

如果仅在函数堆栈上分配它,它将超出范围,并且永远不会调用委托

以下是我在项目中使用的代码,以防万一

import UIKit
//import WebSocket
import Starscream

class ViewController: UIViewController,WebSocketDelegate,WebSocketPongDelegate {

    @IBOutlet weak var wsURL: UITextField!

    @IBOutlet weak var wsConsole: UITextView!

    @IBOutlet weak var wsMessage: UITextField!

    var socket:WebSocket!

    override func viewDidLoad() {
        super.viewDidLoad()

//        var webSocketObj:WebSocket = WebSocket()
//        webSocketObj.ipAddressText = "10.12.1.101"
//        webSocketObj.portText = "8888"
//        webSocketObj.dataToSendText = "hi"
//        webSocketObj.dataRecievedTextView = ""
//        webSocketObj.connectedLabel = ""
//        webSocketObj.connectToServer()


        wsURL.text="ws://10.12.1.101:8888/"
        wsMessage.text="Hi"
        self.navigationItem.leftBarButtonItem?.title="Connect"
        self.navigationItem.rightBarButtonItem?.enabled=false
        // Do any additional setup after loading the view,typically from a nib.
    }

    func websocketDidConnect(socket: WebSocket){
     wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got connected")
         self.navigationItem.leftBarButtonItem?.title="Disconnect"
        self.navigationItem.rightBarButtonItem?.enabled=true
    }

    func websocketDidDisconnect(socket: WebSocket,error: NSError?){
    wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got disconnected")
     self.navigationItem.leftBarButtonItem?.title="Connect"
     self.navigationItem.rightBarButtonItem?.enabled=false
    }

    func websocketDidReceiveMessage(socket: WebSocket,text: String){
     wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket got a message from server:").stringByAppendingString(text)
    }

    func websocketDidReceiveData(socket: WebSocket,data: NSData){
        print("websocket received data",data)
    }

    @IBAction func writeText(sender: UIBarButtonItem) {
        wsConsole.text = wsConsole.text .stringByAppendingString("\n Client sent a message:").stringByAppendingString(wsMessage.text!)
        socket.writeString(wsMessage.text!)
        self.view .endEditing(true)
    }

    @IBAction func disconnect(sender: UIBarButtonItem) {
         self.view .endEditing(true)
        if socket == nil{
            connect(sender)
        }
        else if socket.isConnected {
            socket.disconnect()
        } else {
            connect(sender)
        }
    }

    func connect(sender:UIBarButtonItem){
        socket = WebSocket(url: NSURL(string:wsURL.text!)!)
        socket.delegate = self
        socket.connect()
    }

    func websocketDidReceivePong(socket: WebSocket){
        wsConsole.text = wsConsole.text .stringByAppendingString("\n websocket received pong")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是link到故事板,以防万一你想要

猜你在找的iOS相关文章