Skip to content

Commit

Permalink
Update websockets.md
Browse files Browse the repository at this point in the history
reuse the same socket (i.e. port) for both HTTP traffic and websockets
  • Loading branch information
likev authored May 13, 2024
1 parent 96a0b34 commit 71bc825
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion docs/src/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,29 @@ WebSockets.listen("0.0.0.0", 8080) do ws
send(ws, msg)
end
end
```
```

```julia
# reuse the same socket (i.e. port) for both HTTP traffic and websockets
# https://github.com/JuliaWeb/HTTP.jl/issues/578
server = HTTP.listen() do http::HTTP.Stream
# messy messy code so that we can use the websocket on the same port as the HTTP server
# https://github.com/fonsp/Pluto.jl/blob/main/src/webserver/WebServer.jl#L187-L188
if HTTP.WebSockets.isupgrade(http.message)
HTTP.WebSockets.upgrade(http) do ws
for msg in ws
# simple echo server
send(ws, msg)
end
end
else
function handle(request::HTTP.Request)
return HTTP.Response("Hello")
end

# streamhandler is a middleware that takes a request handler and returns a stream handler.
# https://github.com/JuliaWeb/HTTP.jl/blob/96a0b347cc9e8b001bef4550d5e5ecde9159cc34/src/Handlers.jl#L47-L49
HTTP.streamhandler(handle)(http)
end
end
```

0 comments on commit 71bc825

Please sign in to comment.