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

Ox WebSockets #2187

Merged
merged 22 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
28 changes: 28 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ lazy val allAggregates = projectsWithOptionalNative ++
fs2Ce2.projectRefs ++
fs2.projectRefs ++
monix.projectRefs ++
ox.projectRefs ++
scalaz.projectRefs ++
zio1.projectRefs ++
zio.projectRefs ++
Expand Down Expand Up @@ -231,6 +232,7 @@ lazy val allAggregates = projectsWithOptionalNative ++
slf4jBackend.projectRefs ++
examplesCe2.projectRefs ++
examples.projectRefs ++
examples3.projectRefs ++
docs.projectRefs ++
testServer.projectRefs

Expand Down Expand Up @@ -439,6 +441,17 @@ lazy val monix = (projectMatrix in file("effects/monix"))
settings = commonJsSettings ++ commonJsBackendSettings ++ browserChromeTestSettings ++ testServerSettings
)

lazy val ox = (projectMatrix in file("effects/ox"))
.settings(commonJvmSettings)
.settings(
name := "ox",
libraryDependencies ++= Seq(
"com.softwaremill.ox" %% "core" % "0.1.1"
)
)
.jvmPlatform(scalaVersions = scala3)
.dependsOn(core)

lazy val zio1 = (projectMatrix in file("effects/zio1"))
.settings(
name := "zio1",
Expand Down Expand Up @@ -1038,6 +1051,21 @@ lazy val examples = (projectMatrix in file("examples"))
slf4jBackend
)

lazy val examples3 = (projectMatrix in file("examples3"))
.settings(commonJvmSettings)
.settings(
name := "examples3",
publish / skip := true,
libraryDependencies ++= Seq(
logback
)
)
.jvmPlatform(scalaVersions = scala3)
.dependsOn(
core,
ox
)

//TODO this should be invoked by compilation process, see #https://github.com/scalameta/mdoc/issues/355
val compileDocs: TaskKey[Unit] = taskKey[Unit]("Compiles docs module throwing away its output")
compileDocs := {
Expand Down
53 changes: 53 additions & 0 deletions effects/ox/src/main/scala/sttp/client4/ox/ws/OxWebSockets.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sttp.client4.ox.ws

import ox.*
import ox.channels.Channel
import ox.channels.ChannelClosed
import ox.channels.Sink
import ox.channels.Source
import sttp.client4.ws.SyncWebSocket
import sttp.ws.WebSocketFrame

import scala.util.control.NonFatal

extension (ws: SyncWebSocket)(using Ox) def asSourceAndSink: (Source[WebSocketFrame], Sink[WebSocketFrame]) =
kciesielski marked this conversation as resolved.
Show resolved Hide resolved
val srcChannel = Channel.bufferedDefault[WebSocketFrame]
kciesielski marked this conversation as resolved.
Show resolved Hide resolved
fork {
repeatWhile {
try ws.receive() match
case WebSocketFrame.Close(status, _) if status > 1001 =>
srcChannel.error(new Exception(s"WebSocket closed with status $status"))
false
case _: WebSocketFrame.Close =>
srcChannel.done()
false
case frame =>
srcChannel.send(frame)
true
catch case NonFatal(err) =>
srcChannel.error(err)
false
}
}
val sinkChannel = Channel.bufferedDefault[WebSocketFrame]
fork {
try
repeatWhile {
sinkChannel.receiveOrClosed() match
case closeFrame: WebSocketFrame.Close =>
ws.send(closeFrame) // TODO should we just let 'send' throw exceptions?
false
case frame: WebSocketFrame =>
ws.send(frame)
true
case ChannelClosed.Done =>
ws.send(WebSocketFrame.close)
false
case ChannelClosed.Error(err) =>
// There's no proper "client error" status. Statuses 4000+ are available for custom cases
ws.send(WebSocketFrame.Close(4000, "Client error")) // TODO should we bother the server with client error?
kciesielski marked this conversation as resolved.
Show resolved Hide resolved
false
}
finally uninterruptible(ws.close())
}
(srcChannel, sinkChannel)
31 changes: 31 additions & 0 deletions examples3/src/main/scala/sttp/client4/examples/WebSocketOx.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sttp.client4.examples

import _root_.ox.*
import _root_.ox.channels.Source
import sttp.client4.*
kciesielski marked this conversation as resolved.
Show resolved Hide resolved
import sttp.client4.ox.ws.*
import sttp.client4.ws.SyncWebSocket
import sttp.client4.ws.sync.*
import sttp.ws.WebSocketFrame

@main def wsOxExample =
def useWebSocket(ws: SyncWebSocket): Unit =
supervised {
val inputs = Source.fromValues(1, 2, 3).map(i => WebSocketFrame.text(s"Frame no $i"))
val (wsSource, wsSink) = ws.asSourceAndSink
fork {
inputs.pipeTo(wsSink)
}
wsSource.foreach { frame =>
println(s"RECEIVED: $frame")
}
}

val backend = DefaultSyncBackend()
try
basicRequest
.get(uri"wss://ws.postman-echo.com/raw")
.response(asWebSocket(useWebSocket))
.send(backend).discard
finally
backend.close()
Loading