light Websocket written in Swift base on Swift NIO
- ✅ base on Swift NIO for high-performance
- ✅ user-friendly API
- ✅ auto ping
- [ ] add unit test
- Server
let address: ListeningAddress = .ip(host: "localhost", port: 8000)
WebSocket.server(listen: address, configuration: Configuration(pingInterval: .seconds(10))) { _, websocket in
print("new client connect")
websocket.onText { _, text in
print("recive text from client:", text)
websocket.send(text)
}
websocket.onBinary { _, binary in
var data = binary
if let value = data.readBytes(length: data.readableBytes) {
print("recive binary from client:", String(bytes: value, encoding: .utf8) ?? "")
websocket.send(value)
}
}
_ = websocket.onClose.always { _ in
print("client close connection")
}
}
- client
let address: ServerAddress = .URL(URL: URL(string: "ws://127.0.0.1:\(8000)")!)
if let websocket = WebSocket.client(connect: address, configuration: Configuration(pingInterval: .seconds(10))) {
websocket.onText { _, text in
print("recive text from server:", text)
}
websocket.onBinary { _, binary in
var data = binary
if let value = data.readBytes(length: data.readableBytes) {
print("recive binary from client:", String(bytes: value, encoding: .utf8) ?? "")
}
}
websocket.send("hello test")
websocket.send([UInt8]("hello test".utf8))
try? websocket.onClose.wait()
}
dependencies: [
.package(url: "https://github.com/DanboDuan/swift-websocket.git", .upToNextMajor(from: "1.0.0"))
]