Swift寫(xiě)的WebSocket 客戶端類庫(kù)SwiftWebSocket。
特性:
示例代碼:
func echoTest(){ var messageNum = 0 let ws = WebSocket("wss://echo.websocket.org") let send : ()->() = { let msg = "\(++messageNum): \(NSDate().description)" print("send: \(msg)") ws.send(msg) } ws.event.open = { print("opened") send() } ws.event.close = { code, reason, clean in print("close") } ws.event.error = { error in print("error \(error)") } ws.event.message = { message in if let text = message as? String { print("recv: \(text)") if messageNum == 10 { ws.close() } else { send() } } } }