红蜘蛛代表没有被召唤

时间:2016-05-05 20:41:38

标签: ios swift sockets delegates

变量不是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!")
    }

1 个答案:

答案 0 :(得分:8)

套接字应该是你班级的属性或变量,以确保它能够存在。

如果仅在函数堆栈上分配它,它将超出范围,代理将永远不会被调用

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

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到故事板