Skip to content

Commit

Permalink
feat: extend example
Browse files Browse the repository at this point in the history
  • Loading branch information
koraykoska committed Aug 12, 2023
1 parent 9646eac commit ccb53f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Examples/EchoClient/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "/Users/koray/git/libwebsockets.swift",
"state" : {
"branch" : "master",
"revision" : "003345e045d146c10423ddf7c36cdfc305f55818"
"revision" : "9646eac5a011ba99264d69e14aacc74b4d0a0d68"
}
},
{
Expand Down
6 changes: 6 additions & 0 deletions Examples/EchoClient/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import PackageDescription

let package = Package(
name: "EchoClient",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13),
],
dependencies: [
.package(url: "../../", branch: "master"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.57.0"),
Expand Down
70 changes: 33 additions & 37 deletions Examples/EchoClient/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import NIOPosix
print("Running Echo Client")

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
func createWebsocket() -> WebsocketClient {
let connectionPromise = eventLoopGroup.next().makePromise(of: Void.self)

func createWebsocket() -> EventLoopFuture<WebsocketClient> {
// let websocket = try! WebsocketClient(
// scheme: .wss,
// host: "ws.postman-echo.com",
Expand All @@ -24,7 +22,7 @@ func createWebsocket() -> WebsocketClient {
// eventLoop: eventLoopGroup.next(),
// onConnect: connectionPromise
// )
let websocket = try! WebsocketClient(
let websocketPromise = WebsocketClient.connect(
scheme: .ws,
host: "127.0.0.1",
port: 10000,
Expand All @@ -37,39 +35,33 @@ func createWebsocket() -> WebsocketClient {
permessageDeflate: true,
connectionTimeoutSeconds: 5,
eventLoop: eventLoopGroup.next(),
onConnect: connectionPromise
)
websocket.onClose { reason in
print("Close \(reason)")
}
websocket.onFragment { client, data, a, b, c in
print("Recv: \(data.count) bytes")
}
websocket.onBinary { ws, data in
print("Received binary")
print("\(data.count)")
}
websocket.onText { ws, text in
print("Received text")
print(text)
}
websocket.onPong { ws, data in
print("pong received")
print("\(data.count)")
}

_ = connectionPromise.futureResult.always { [weak websocket] result in
guard let websocket else {
return
onText: { ws, text in
print("Received text")
print(text)
},
onBinary: { ws, data in
print("Received binary")
print("\(data.count)")
},
onFragment: { client, data, a, b, c in
print("Recv: \(data.count) bytes")
},
onPong: { ws, data in
print("pong received")
print("\(data.count)")
},
onClose: { reason in
print("Close \(reason)")
}

)
return websocketPromise.always { result in
switch result {
case .failure(let err):
print(err)
case .success:
case .success(let websocket):
print("Connection success")
var longString = ""
for _ in 0..<10 {
for _ in 0..<30 {
longString += "\(UUID().uuidString)"
}
websocket.send(longString.data(using: .utf8)!, opcode: .text)
Expand All @@ -88,14 +80,18 @@ func createWebsocket() -> WebsocketClient {
// websocket.send(Data(), opcode: .ping)
}
}

return websocket
}
eventLoopGroup.next().scheduleTask(in: .seconds(5), {
let ws = createWebsocket()
eventLoopGroup.next().scheduleTask(in: .seconds(5), {
_ = ws.isClosed
})
createWebsocket().always { result in
switch result {
case .success(let ws):
eventLoopGroup.next().scheduleTask(in: .seconds(5), {
_ = ws.isClosed
})
default:
break
}
}
})

RunLoop.main.run()

0 comments on commit ccb53f9

Please sign in to comment.