DNWebSocket 1.1.0

DNWebSocket 1.1.0

Gleb Radchenko 维护。



  • Gleb Radchenko

DNWebSocket

Swift 4.0 MIT

面向对象、Swift风格的WebSocket库,适用于Swift兼容的平台。(RFC 6455

测试

符合所有必要的 Autobahn 糟糕测试。 Autobahn

您可以在这里查看DNWebSocket的测试结果 这里

SocketRocket相比,该库在许多边限/性能测试中表现出2-10倍的性能提升。

由于性能改进,第6.4.1、6.4.2、6.4.3和6.4.4个案例收到了非严格的结果(因为要验证每个分段的文本消息很复杂)。

安装

Cocoapods

要使用 CocoaPods 安装 DNWebSocket,请获取它

$ gem install cocoapods

然后,在您的项目根目录下创建一个 Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Target Name>' do
    pod 'DNWebSocket', '~> 1.0.2'
end

然后运行

$ pod install

Swift 包管理器

目前,我正在寻找一种通用的方法,允许在不同的包管理器中使用 C 库。所以,请现在使用 DNWebSocket-SPM 存储库。

要求

  • iOS 8.0+/macOS 10.10+/tvOS 9.0+/watchOS 2.0+
  • Swift 4.0+(但顺带说一下,我还没有尝试过之前的版本 :D)

使用说明

按照以下方式导入库

import DNWebSocket

现在创建 websocket,配置它并连接

let websocket = WebSocket(url: URL(string: "wss://echo.websocket.org:80")!,
                          timeout: 10,
                          protocols: ["chat", "superchat"])

websocket.onConnect = {
    print("connected")
    websocket.sendPing(data: Data())
}

websocket.onData = { (data) in
    websocket.send(data: data)
}

websocket.onText = { (text) in
    websocket.send(string: text)
}

websocket.onPing = { (data) in
    websocket.sendPong(data: data)
}

websocket.onPong = { (data) in
    print("Received pong from server")
}

websocket.onDebugInfo = { (debugInfo) in
    print(debugInfo)
}

websocket.onDisconnect = { (error, closeCode) in
    print("disconnected: \(closeCode)")
}

websocket.connect()

你可以通过访问 .settings 和 .securitySettings 属性创建自定义连接

websocket.settings.timeout = 5 // sec
websocket.settings.debugMode = true // will trigger .onDebugInfo callback and send .debug(String) event
websocket.settings.useCompression = true // false by default
websocket.settings.maskOutputData = true // true by default
websocket.settings.respondPingRequestsAutomatically = true // true by default 
websocket.settings.callbackQueue = .main

websocket.securitySettings.useSSL = false // true by default
websocket.securitySettings.overrideTrustHostname = true // false by default
websocket.securitySettings.trustHostname = /*your hostname*/
websocket.securitySettings.certificateValidationEnabled = true
websocket.securitySettings.cipherSuites = []