-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of websocket support for ZIO http. #3147
Conversation
4230f9f
to
97444d8
Compare
Yes, this is definitely the right direction! Though so far, I think this will support only a single incoming message producing multiple outgoing messages? |
Thank Adam for the quick feedback. Yes, that was correct, I have updated the code a bit to handle continuation frames and few other cases. I have one question, this test is failing locally, and to be honest, it is not entirely clear to me what is the expected behavior, it would be great if you help me understand. Thanks again, really appreciate the support. |
bc6c104
to
985b653
Compare
Diving a bit in the websocket implementation, I think there is a potential limitation (bug?). AFAIU, it is always the expectation to receive at least on This use case explains the issue a bit better. testServer(
endpoint.out(webSocketBody[String, CodecFormat.TextPlain, Option[String], CodecFormat.TextPlain](streams)),
"Only server send events"
)((_: Unit) => pureResult(functionToPipe[String, Option[String]] { _ => Some("test") }.asRight[Unit])) { (backend, baseUri) =>
basicRequest
.response(asWebSocket { (ws: WebSocket[IO]) =>
for {
m1 <- ws.eitherClose(ws.receiveText())
m2 <- ws.eitherClose(ws.receiveText())
m3 <- ws.eitherClose(ws.receiveText())
_ <- ws.close()
} yield List(m1, m2, m3)
})
.get(baseUri.scheme("ws"))
.send(backend)
.map(
_.body.map(_.map(_.left.map(_.statusCode))) shouldBe Right(
List(Right("test"), Right("test"), Right("test"), Left(WebSocketFrame.close.statusCode))
)
)
},
|
As for the test you mentioned, the scenario there is that a client connects, but the server always responds with an empty stream. "End of stream" means in case of web sockets "close the connection", so the only expected communication is a close frame, sent from the server to the client. |
For the second question, I think the problem is here: functionToPipe[String, Option[String]] { _ => Some("test") } The server-side behavior is to create a pipe, which maps each incoming message to To produce some "welcome" messages, I think you should do sth along the lines of: (in: ZStream[Any, Nothing, String]) => ZStream.fromIterable(List("hello", "world")) ++ in So for a given stream of incoming messages, you return a stream which emits two values (hello world), and then echoes any of the incoming ones. |
365c17b
to
33c4b5e
Compare
94e8030
to
e922456
Compare
e922456
to
c5933c9
Compare
Looks great, thank you! |
This PR is roughly based on this one. It aims to introduce web socket support for zio http.
TODO:
Add tests for fragmented framesWill do this in a follow up PR