Skip to content

Commit

Permalink
Revert "Add description and client code to websocket example (#1418)"
Browse files Browse the repository at this point in the history
This reverts commit 9a2364f.
  • Loading branch information
987Nabil committed Dec 24, 2023
1 parent 9a2364f commit 6d4619d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 181 deletions.
61 changes: 61 additions & 0 deletions docs/examples/advanced/websocket-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
id: websocket-server
title: "WebSocket Server Example"
sidebar_label: "WebSocket Server"
---

```scala mdoc:silent
import zio._

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

object WebSocketAdvanced extends ZIOAppDefault {

val socketApp: WebSocketApp[Any] =
Handler.webSocket { channel =>
channel.receiveAll {
case Read(WebSocketFrame.Text("end")) =>
channel.shutdown

// Send a "bar" if the server sends a "foo"
case Read(WebSocketFrame.Text("foo")) =>
channel.send(Read(WebSocketFrame.text("bar")))

// Send a "foo" if the server sends a "bar"
case Read(WebSocketFrame.Text("bar")) =>
channel.send(Read(WebSocketFrame.text("foo")))

// Echo the same message 10 times if it's not "foo" or "bar"
case Read(WebSocketFrame.Text(text)) =>
channel.send(Read(WebSocketFrame.text(text))).repeatN(10)

// Send a "greeting" message to the server once the connection is established
case UserEventTriggered(UserEvent.HandshakeComplete) =>
channel.send(Read(WebSocketFrame.text("Greetings!")))

// Log when the channel is getting closed
case Read(WebSocketFrame.Close(status, reason)) =>
Console.printLine("Closing channel with status: " + status + " and reason: " + reason)

// Print the exception if it's not a normal close
case ExceptionCaught(cause) =>
Console.printLine(s"Channel error!: ${cause.getMessage}")

case _ =>
ZIO.unit
}
}

val app: HttpApp[Any] =
Routes(
Method.GET / "greet" / string("name") -> handler { (name: String, req: Request) =>
Response.text(s"Greetings ${name}!")
},
Method.GET / "subscriptions" -> handler(socketApp.toResponse),
).toHttpApp

override val run = Server.serve(app).provide(Server.default)
}
```
138 changes: 0 additions & 138 deletions docs/examples/advanced/websocket.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const sidebars = {
"examples/advanced/server",
"examples/advanced/streaming-file",
"examples/advanced/streaming-response",
"examples/advanced/websocket"
"examples/advanced/websocket-server"
]
}
]
Expand Down
45 changes: 3 additions & 42 deletions zio-http-example/src/main/scala/example/WebSocketAdvanced.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ object WebSocketAdvanced extends ZIOAppDefault {
case Read(WebSocketFrame.Text("end")) =>
channel.shutdown

// Send a "bar" if the client sends a "foo"
// Send a "bar" if the server sends a "foo"
case Read(WebSocketFrame.Text("foo")) =>
channel.send(Read(WebSocketFrame.text("bar")))

// Send a "foo" if the client sends a "bar"
// Send a "foo" if the server sends a "bar"
case Read(WebSocketFrame.Text("bar")) =>
channel.send(Read(WebSocketFrame.text("foo")))

Expand All @@ -31,7 +31,7 @@ object WebSocketAdvanced extends ZIOAppDefault {
ZIO.logErrorCause(s"failed sending", cause)
}

// Send a "greeting" message to the client once the connection is established
// Send a "greeting" message to the server once the connection is established
case UserEventTriggered(UserEvent.HandshakeComplete) =>
channel.send(Read(WebSocketFrame.text("Greetings!")))

Expand All @@ -58,42 +58,3 @@ object WebSocketAdvanced extends ZIOAppDefault {

override val run = Server.serve(app).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")

override val run =
(for {
_ <- webSocketHandler
_ <- Console.readLine.flatMap(sendChatMessage).forever.forkDaemon
_ <- ZIO.never
} yield ())
.provideSome[Scope](
Client.default,
ZLayer(Queue.bounded[String](100)),
)

}

0 comments on commit 6d4619d

Please sign in to comment.