Swocket 0.1.0

Swocket 0.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2015年6月
SPM支持 SPM

Joakim Gyllström 维护。



Swocket 0.1.0

  • Joakim Gyllström

待办事项

  • UDP

使用方法

要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install

回显客户端
let data = "Wazzzup".dataUsingEncoding(NSUTF8StringEncoding)!

// Set up a socket to localhost on port 9999
let client = Swocket.TCP.init(host: "127.0.0.1", port: 9999)

// Connect to server
client.connectAsync()

// Send message
client.sendDataAsync(data)

// Get response
client.recieveDataAsync({ (data, error) -> Void in
    // Unwrap response as string and set response label
    let response = NSString(data: data!, encoding: NSUTF8StringEncoding) as? String
    print(response)
})

// Disconnect
client.disconnectAsync()
HTTP 服务器
let httpString = "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8"
let htmlString = "<html><head><title>Hello</title></head><body><h1>Hello World!</h1><p>I am a tiny little web server</p></body></html>"
let data = "\(httpString)\n\n\(htmlString)".dataUsingEncoding(NSUTF8StringEncoding)!

server = try! Swocket.TCP.listen(8080, onConnection: { (client) -> () in
    try! client.recieveData() // Ignore what client requests
    try! client.sendData(data) // And give them the same result every time! :P
})
想要同步处理事情?

没问题,所有异步函数都有一个同步对应版本。

处理错误
// All async functions have an optional error closure
client.sendDataAsync(data, onError: { (error) -> Void in
  print(error)
})

// And synchronous functions throws an error
do {
  try client.sendData(data)
} catch {
  print(error)
}

要求

Xcode 7(Swift 2)

安装

通过 CocoaPods 可以获得 Swocket。要安装它,只需将以下行添加到您的 Podfile 。

pod "Swocket"

作者

Joakim Gyllström,[email protected]

许可证

Swocket 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。