From cb9d9f9d75269bd6a56739bfa28bdda0f81a7882 Mon Sep 17 00:00:00 2001 From: Milad Khajavi Date: Sat, 28 Dec 2024 10:08:44 +0330 Subject: [PATCH 1/4] fix issues with line numbering. --- docs/examples/websocket.md | 8 +-- docs/reference/client.md | 2 +- .../websocket/WebSocketClientAdvanced.scala | 47 ++++++++++++++++++ .../{ => websocket}/WebSocketEcho.scala | 3 +- .../WebSocketReconnectingClient.scala | 3 +- .../WebSocketServerAdvanced.scala} | 49 ++----------------- .../WebSocketSimpleClient.scala | 3 +- .../http/netty/server/ServerSSLDecoder.scala | 2 +- 8 files changed, 60 insertions(+), 57 deletions(-) create mode 100644 zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala rename zio-http-example/src/main/scala/example/{ => websocket}/WebSocketEcho.scala (97%) rename zio-http-example/src/main/scala/example/{ => websocket}/WebSocketReconnectingClient.scala (98%) rename zio-http-example/src/main/scala/example/{WebSocketAdvanced.scala => websocket/WebSocketServerAdvanced.scala} (61%) rename zio-http-example/src/main/scala/example/{ => websocket}/WebSocketSimpleClient.scala (97%) diff --git a/docs/examples/websocket.md b/docs/examples/websocket.md index 52d568361..06537d1cd 100644 --- a/docs/examples/websocket.md +++ b/docs/examples/websocket.md @@ -21,12 +21,12 @@ Our WebSocketApp will handle the following events send by the client: * If the client sends "end", we will close the connection. * If the client sends any other message, we will send the same message back to the client 10 times. -For the client to establish a connection with the server, we offer the `/subscriptions` endpoint. +For the client to establish a connection with the server, we offer the `/subscriptions` endpoint: ```scala mdoc:passthrough import utils._ -printSource("zio-http-example/src/main/scala/example/WebSocketAdvanced.scala", lines=Seq((3, 7), (9, 60)), showLineNumbers=false) +printSource("zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala") ``` A few things worth noting: @@ -47,7 +47,7 @@ All we need for that, is the URL of the server. In our case it's `"ws://localhos ```scala mdoc:passthrough import utils._ -printSource("zio-http-example/src/main/scala/example/WebSocketAdvanced.scala", lines=Seq((3, 7), (62, 99)), showLineNumbers=false) +printSource("zio-http-example/src/main/scala/example/webSocket/WebSocketClientAdvanced.scala") ``` While we access here `Queue[String]` via the ZIO environment, you should use a service in a real world application, that requires a queue as one of its constructor dependencies. @@ -59,5 +59,5 @@ See [ZIO Services](https://zio.dev/reference/service-pattern/) for more informat ```scala mdoc:passthrough import utils._ -printSource("zio-http-example/src/main/scala/example/WebSocketEcho.scala") +printSource("zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala") ``` diff --git a/docs/reference/client.md b/docs/reference/client.md index e334de922..fad0a1a28 100644 --- a/docs/reference/client.md +++ b/docs/reference/client.md @@ -511,5 +511,5 @@ This example represents a WebSocket client application that automatically attemp ```scala mdoc:passthrough import utils._ -printSource("zio-http-example/src/main/scala/example/WebSocketReconnectingClient.scala") +printSource("zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala") ``` diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala new file mode 100644 index 000000000..1ae10335c --- /dev/null +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala @@ -0,0 +1,47 @@ +package example.websocket +import zio._ +import zio.http.ChannelEvent.Read +import zio.http._ + +import scala.annotation.nowarn + +object WebSocketSimpleClient extends ZIOAppDefault { + + def sendChatMessage(message: String): ZIO[Queue[String], Throwable, Unit] = + ZIO.serviceWithZIO[Queue[String]](_.offer(message).unit) + + def processQueue(channel: WebSocketChannel): ZIO[Queue[String], Throwable, Unit] = { + for { + queue <- ZIO.service[Queue[String]] + msg <- queue.take + _ <- channel.send(Read(WebSocketFrame.Text(msg))) + } yield () + }.forever.forkDaemon.unit + + private def webSocketHandler: ZIO[Queue[String] with Client with Scope, Throwable, Response] = + Handler.webSocket { channel => + for { + _ <- processQueue(channel) + _ <- channel.receiveAll { + case Read(WebSocketFrame.Text(text)) => + Console.printLine(s"Server: $text") + case _ => + ZIO.unit + } + } yield () + }.connect("ws://localhost:8080/subscriptions") + + @nowarn("msg=dead code") + override val run = + ZIO + .scoped(for { + _ <- webSocketHandler + _ <- Console.readLine.flatMap(sendChatMessage).forever.forkDaemon + _ <- ZIO.never + } yield ()) + .provide( + Client.default, + ZLayer(Queue.bounded[String](100)), + ) + +} diff --git a/zio-http-example/src/main/scala/example/WebSocketEcho.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala similarity index 97% rename from zio-http-example/src/main/scala/example/WebSocketEcho.scala rename to zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala index 245c722f3..eefd0809d 100644 --- a/zio-http-example/src/main/scala/example/WebSocketEcho.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala @@ -1,7 +1,6 @@ -package example +package example.websocket import zio._ - import zio.http.ChannelEvent.Read import zio.http._ import zio.http.codec.PathCodec.string diff --git a/zio-http-example/src/main/scala/example/WebSocketReconnectingClient.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala similarity index 98% rename from zio-http-example/src/main/scala/example/WebSocketReconnectingClient.scala rename to zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala index 289267524..f671f8862 100644 --- a/zio-http-example/src/main/scala/example/WebSocketReconnectingClient.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala @@ -1,7 +1,6 @@ -package example +package example.websocket import zio._ - import zio.http.ChannelEvent.{ExceptionCaught, Read, UserEvent, UserEventTriggered} import zio.http._ diff --git a/zio-http-example/src/main/scala/example/WebSocketAdvanced.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala similarity index 61% rename from zio-http-example/src/main/scala/example/WebSocketAdvanced.scala rename to zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala index 72b90aab7..93f5dd221 100644 --- a/zio-http-example/src/main/scala/example/WebSocketAdvanced.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala @@ -1,14 +1,13 @@ -package example - -import scala.annotation.nowarn +package example.websocket import zio._ - import zio.http.ChannelEvent.{ExceptionCaught, Read, UserEvent, UserEventTriggered} import zio.http._ import zio.http.codec.PathCodec.string -object WebSocketAdvanced extends ZIOAppDefault { +import scala.annotation.nowarn + +object WebSocketServerAdvanced extends ZIOAppDefault { val socketApp: WebSocketApp[Any] = Handler.webSocket { channel => @@ -61,43 +60,3 @@ object WebSocketAdvanced extends ZIOAppDefault { override val run = Server.serve(routes).provide(Server.default) } -object WebSocketAdvancedClient extends ZIOAppDefault { - - def sendChatMessage(message: String): ZIO[Queue[String], Throwable, Unit] = - ZIO.serviceWithZIO[Queue[String]](_.offer(message).unit) - - def processQueue(channel: WebSocketChannel): ZIO[Queue[String], Throwable, Unit] = { - for { - queue <- ZIO.service[Queue[String]] - msg <- queue.take - _ <- channel.send(Read(WebSocketFrame.Text(msg))) - } yield () - }.forever.forkDaemon.unit - - private def webSocketHandler: ZIO[Queue[String] with Client with Scope, Throwable, Response] = - Handler.webSocket { channel => - for { - _ <- processQueue(channel) - _ <- channel.receiveAll { - case Read(WebSocketFrame.Text(text)) => - Console.printLine(s"Server: $text") - case _ => - ZIO.unit - } - } yield () - }.connect("ws://localhost:8080/subscriptions") - - @nowarn("msg=dead code") - override val run = - ZIO - .scoped(for { - _ <- webSocketHandler - _ <- Console.readLine.flatMap(sendChatMessage).forever.forkDaemon - _ <- ZIO.never - } yield ()) - .provide( - Client.default, - ZLayer(Queue.bounded[String](100)), - ) - -} diff --git a/zio-http-example/src/main/scala/example/WebSocketSimpleClient.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala similarity index 97% rename from zio-http-example/src/main/scala/example/WebSocketSimpleClient.scala rename to zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala index 9e7dd8f17..9ad83a504 100644 --- a/zio-http-example/src/main/scala/example/WebSocketSimpleClient.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala @@ -1,7 +1,6 @@ -package example +package example.websocket import zio._ - import zio.http.ChannelEvent.{Read, UserEvent, UserEventTriggered} import zio.http._ diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala index c3f9a6967..53c3dfb3f 100644 --- a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala +++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala @@ -131,7 +131,7 @@ private[zio] class ServerSSLDecoder(sslConfig: SSLConfig, cfg: Server.Config) ex val httpBehaviour = sslConfig.behaviour if (in.readableBytes < 5) () - else if (SslHandler.isEncrypted(in, false)) { + else if (SslHandler.isEncrypted(in)) { pipeline.replace(this, Names.SSLHandler, sslContext.newHandler(context.alloc())) () } else { From 20beeb3007a6a130d61d3cb4fd414a393f6a365e Mon Sep 17 00:00:00 2001 From: Milad Khajavi Date: Sat, 28 Dec 2024 10:13:00 +0330 Subject: [PATCH 2/4] revert unwanted changes. --- .../src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala index 53c3dfb3f..c3f9a6967 100644 --- a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala +++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerSSLDecoder.scala @@ -131,7 +131,7 @@ private[zio] class ServerSSLDecoder(sslConfig: SSLConfig, cfg: Server.Config) ex val httpBehaviour = sslConfig.behaviour if (in.readableBytes < 5) () - else if (SslHandler.isEncrypted(in)) { + else if (SslHandler.isEncrypted(in, false)) { pipeline.replace(this, Names.SSLHandler, sslContext.newHandler(context.alloc())) () } else { From b99bd4830047de9e3be39baae61687f556cb9b5c Mon Sep 17 00:00:00 2001 From: Milad Khajavi Date: Mon, 6 Jan 2025 11:50:11 +0330 Subject: [PATCH 3/4] fmt. --- .../scala/example/websocket/WebSocketClientAdvanced.scala | 7 ++++--- .../src/main/scala/example/websocket/WebSocketEcho.scala | 1 + .../example/websocket/WebSocketReconnectingClient.scala | 1 + .../scala/example/websocket/WebSocketServerAdvanced.scala | 6 +++--- .../scala/example/websocket/WebSocketSimpleClient.scala | 1 + 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala index 1ae10335c..8247b0195 100644 --- a/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala @@ -1,11 +1,12 @@ package example.websocket +import scala.annotation.nowarn + import zio._ + import zio.http.ChannelEvent.Read import zio.http._ -import scala.annotation.nowarn - -object WebSocketSimpleClient extends ZIOAppDefault { +object WebSocketSimpleClientAdvanced extends ZIOAppDefault { def sendChatMessage(message: String): ZIO[Queue[String], Throwable, Unit] = ZIO.serviceWithZIO[Queue[String]](_.offer(message).unit) diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala index eefd0809d..0ba5dc00f 100644 --- a/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketEcho.scala @@ -1,6 +1,7 @@ package example.websocket import zio._ + import zio.http.ChannelEvent.Read import zio.http._ import zio.http.codec.PathCodec.string diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala index f671f8862..599ac554e 100644 --- a/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketReconnectingClient.scala @@ -1,6 +1,7 @@ package example.websocket import zio._ + import zio.http.ChannelEvent.{ExceptionCaught, Read, UserEvent, UserEventTriggered} import zio.http._ diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala index 93f5dd221..19a98f343 100644 --- a/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketServerAdvanced.scala @@ -1,12 +1,13 @@ package example.websocket +import scala.annotation.nowarn + import zio._ + import zio.http.ChannelEvent.{ExceptionCaught, Read, UserEvent, UserEventTriggered} import zio.http._ import zio.http.codec.PathCodec.string -import scala.annotation.nowarn - object WebSocketServerAdvanced extends ZIOAppDefault { val socketApp: WebSocketApp[Any] = @@ -59,4 +60,3 @@ object WebSocketServerAdvanced extends ZIOAppDefault { override val run = Server.serve(routes).provide(Server.default) } - diff --git a/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala b/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala index 9ad83a504..798935e6c 100644 --- a/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala +++ b/zio-http-example/src/main/scala/example/websocket/WebSocketSimpleClient.scala @@ -1,6 +1,7 @@ package example.websocket import zio._ + import zio.http.ChannelEvent.{Read, UserEvent, UserEventTriggered} import zio.http._ From 1575c6276ca011aa438ec2fbe2bc9b15df525161 Mon Sep 17 00:00:00 2001 From: Milad Khajavi Date: Mon, 6 Jan 2025 15:43:52 +0330 Subject: [PATCH 4/4] fix example url. --- docs/examples/websocket.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/websocket.md b/docs/examples/websocket.md index 06537d1cd..a49a85ef6 100644 --- a/docs/examples/websocket.md +++ b/docs/examples/websocket.md @@ -47,7 +47,7 @@ All we need for that, is the URL of the server. In our case it's `"ws://localhos ```scala mdoc:passthrough import utils._ -printSource("zio-http-example/src/main/scala/example/webSocket/WebSocketClientAdvanced.scala") +printSource("zio-http-example/src/main/scala/example/websocket/WebSocketClientAdvanced.scala") ``` While we access here `Queue[String]` via the ZIO environment, you should use a service in a real world application, that requires a queue as one of its constructor dependencies.