-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Add description and client code to websocket example (#1418)"
This reverts commit 9a2364f.
- Loading branch information
Showing
4 changed files
with
65 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters