Skip to content

Commit

Permalink
Forward left over bytes to websocket handler (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Feb 21, 2023
1 parent 33c60b6 commit 8cb4bf5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/HummingbirdWSClient/WebSocketClient.swift
Expand Up @@ -104,7 +104,7 @@ public enum HBWebSocketClient {
)

// add HTTP handler with web socket upgrade
return channel.pipeline.addHTTPClientHandlers(withClientUpgrade: config).flatMap {
return channel.pipeline.addHTTPClientHandlers(leftOverBytesStrategy: .forwardBytes, withClientUpgrade: config).flatMap {
channel.pipeline.addHandler(httpHandler)
}
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/HummingbirdWSCore/HTTPServer+WebSocket.swift
Expand Up @@ -32,8 +32,9 @@ extension HBHTTPServer {
},
upgradePipelineHandler: { (channel: Channel, head: HTTPRequestHead) in
let webSocket = HBWebSocket(channel: channel, type: .server)
onUpgrade(webSocket, head)
return channel.pipeline.addHandler(WebSocketHandler(webSocket: webSocket))
return channel.pipeline.addHandler(WebSocketHandler(webSocket: webSocket)).map { _ in
onUpgrade(webSocket, head)
}
}
)
self.httpChannelInitializer.addProtocolUpgrader(upgrader)
Expand Down
20 changes: 19 additions & 1 deletion Tests/HummingbirdWebSocketTests/WebSocketTests.swift
Expand Up @@ -23,7 +23,7 @@ final class HummingbirdWebSocketTests: XCTestCase {
static var eventLoopGroup: EventLoopGroup!

override class func setUp() {
Self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
Self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
}

override class func tearDown() {
Expand Down Expand Up @@ -124,6 +124,24 @@ final class HummingbirdWebSocketTests: XCTestCase {
XCTAssertTrue(clientHello)
}

func testServerImmediateWrite() throws {
let promise = TimeoutPromise(eventLoop: Self.eventLoopGroup.next(), timeout: .seconds(60))
let app = try self.setupClientAndServer(
onServer: { ws in
ws.write(.text("hello"), promise: nil)
},
onClient: { ws in
ws.onRead { data, _ in
XCTAssertEqual(data, .text("hello"))
promise.succeed()
}
}
)
defer { app.stop() }

try promise.wait()
}

/* Commented out as ws://echo.websocket.org is not working anymore
func testClient() throws {
let eventLoop = Self.eventLoopGroup.next()
Expand Down

0 comments on commit 8cb4bf5

Please sign in to comment.