2

我正在尝试使用 WebSocket 功能创建一个单例,但我只是不知道为什么它不起作用

这是我的单例扩展:

import Starscream

extension WebSocketManager: WebSocketDelegate {
func websocketDidConnect(socket: WebSocket) {
    print("websocket is connected")
}

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

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

func websocketDidReceiveData(socket: WebSocket, data: Data) {
    print("Received data: \(data.count)")
}}

我的单身人士:

final class WebSocketManager: NSObject {
static let sharedInstance = WebSocketManager()
public var socket = WebSocket(url: URL(string: "ws://t-w-a.herokuapp.com/new")!)

private override init() {
    super.init()
    // testing connection of the socket
}

func establishConnection() {
    socket.connect()
}

func closeConnection() {
    socket.disconnect()
}}

当我尝试连接服务器时,什么也没有发生。这是我的控制器:

class ChatViewController: UIViewController {

@IBOutlet weak var typeText: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    WebSocketManager.sharedInstance.establishConnection()
}}
4

1 回答 1

1

我发现了问题,我只是忘记为委托分配套接字变量

于 2017-05-23T23:47:32.080 回答