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

Update websockets.md #1179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
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
```
Loading