Skip to content
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

Fix Issues with Line Numbering in Documentation Examples #3265

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/examples/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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")
```
2 changes: 1 addition & 1 deletion docs/reference/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```
Original file line number Diff line number Diff line change
@@ -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)),
)

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package example
package example.websocket

import zio._

import zio.http.ChannelEvent.{ExceptionCaught, Read, UserEvent, UserEventTriggered}
import zio.http._

Expand Down
Original file line number Diff line number Diff line change
@@ -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 =>
Expand Down Expand Up @@ -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)),
)

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package example
package example.websocket

import zio._

import zio.http.ChannelEvent.{Read, UserEvent, UserEventTriggered}
import zio.http._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
khajavi marked this conversation as resolved.
Show resolved Hide resolved
pipeline.replace(this, Names.SSLHandler, sslContext.newHandler(context.alloc()))
()
} else {
Expand Down
Loading