WebSocket 客户端库:SwiftWebSocket

m45y 6年前

SwiftWebSocket 是高性能的 WebSocket 客户端库。

特性:

  • Pure Swift solution. No need for Objective-C Bridging.

  • Reads compressed messages (permessage-deflate). IETF Draft

  • Strict UTF-8 processing.

  • The API is modeled after the Javascript API.

  • TLS / WSS support.

  • binaryTypeproperty to choose between[UInt8]orNSDatamessages.

  • Zero asserts. All networking, stream, and protocol errors are routed through theerrorevent.

  • Send pings and receive pong events.

  • High performance.

例子:

func echoTest(){      var messageNum = 1      var ws = WebSocket(url: "wss://echo.websocket.org")      var send : ()->() = {          var msg = "#\(messageNum++): \(NSDate().description)"          println("send: \(msg)")          ws.send(msg)      }      ws.event.open = {          println("opened")          send()      }      ws.event.close = { (code, reason, clean) in          println("close")      }      ws.event.error = { (error) in          println("error \(error.localizedDescription)")      }      ws.event.message = { (message) in          if let text = message as? String {              println("recv: \(text)")              send()          }      }  }

项目主页:http://www.open-open.com/lib/view/home/1433145778150