Group Group Group Group Group Group Group Group Group

Chapter 29: WebSockets

Really appreciate the book, I have been writing robust server side swift code backed by your unittest demos.
However when I try to do TDD to implement chat component based on the websockets. I met some trouble:
I have a echo websocket at ws://127.0.0.1:8080
Here is my testcase:

func testWSSocket() throws {
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)
//let ws = try HTTPClient.webSocket(scheme: .ws, hostname: “127.0.0.1”, port: 8080, on: worker).wait()
let ws = try app.client().webSocket(“ws://127.0.0.1:8080”).wait()

    let promise = worker.eventLoop.newPromise(String.self)
    ws.onText { ws, text in
        promise.succeed(result: text)
        ws.close(code: .normalClosure)
    }
    ws.onCloseCode { code in
        print("code: \(code)")
    }
    let message = "Hello, world!"
    ws.send(message)
    try XCTAssertEqual(promise.futureResult.wait(), message)
    try ws.onClose.wait()
}

I ran the case, it always throw this error:

▿ NIOConnectionError

  • host : “127.0.0.1”
  • port : 8080
  • dnsAError : nil
  • dnsAAAAError : nil
    ▿ connectionErrors : 1 element
    ▿ 0 : SingleConnectionFailure
    ▿ target : [IPv4]127.0.0.1/127.0.0.1:8080
    ▿ v4 : IPv4Address
    ▿ _storage : <Box<(address: sockaddr_in, host: String)>: 0x100c45470>
    ▿ error : connection reset (error set): Connection refused (errno: 61)
    - errnoCode : 61
    ▿ reason : FailureDescription
    - reason : "connection reset (error set)

But I tried with the wsta command, it connects and works fine.
Can you provide a unittest demo for this chapter?

Thank you

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Just for posterity - the conclusion was there’s no way to do this without wide integration tests like the NIO web socket tests