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

Add host kwarg to open() and add some documentation #89

Merged
merged 5 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/MeshCat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using Base.Filesystem: rm
using BinDeps: download_cmd, unpack_cmd
using UUIDs: UUID, uuid1
using LinearAlgebra: UniformScaling, norm
using Sockets: listen
using Sockets: listen, @ip_str, IPAddr, IPv4, IPv6
using Base64: base64encode
using MsgPack: MsgPack, pack, Ext

Expand Down
30 changes: 24 additions & 6 deletions src/servers.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@

"""
open(vis::Visualizer; host::IPAddr = ip"127.0.0.1", start_browser = true,
default_port = 8700, max_retries = 500)

Start a server for the visualizer so that it can be accessed from a browser
using the given host URL. This method will try to search for an open port,
starting at `default_port` and then trying `max_retries` additional port
numbers. If `start_browser` is true, then a new browser window will be opened.

open(vis::Visualizer, port::Integer; host::IPAddr = ip"127.0.0.1", start_browser = true)

Start a server for the visualizer so that it can be accessed from a browser
using the given host URL and port. If `start_browser` is true, then a new
browser window will be opened.

"""
Base.open(vis::Visualizer, args...; kw...) = open(vis.core, args...; kw...)

function Base.open(core::CoreVisualizer, port::Integer; start_browser = true)
@async WebIO.webio_serve(Mux.page("/", req -> core.scope), port)
url = "http://127.0.0.1:$port"
function Base.open(core::CoreVisualizer, port::Integer;
host::IPAddr = ip"127.0.0.1", start_browser = true)
@async WebIO.webio_serve(Mux.page("/", req -> core.scope), host, port)
url = "http://$host:$port"
@info("Serving MeshCat visualizer at $url")
start_browser && open_url(url)
end
Expand All @@ -24,10 +41,11 @@ function open_url(url)
end
end

function Base.open(core::CoreVisualizer; default_port=8700, max_retries=500, start_browser = true)
function Base.open(core::CoreVisualizer;
host::IPAddr=ip"127.0.0.1", default_port=8700, max_retries=500, kw...)
for port in default_port:(default_port + max_retries)
server = try
listen(port)
listen(host, port)
catch e
if e isa Base.IOError
continue
Expand All @@ -38,6 +56,6 @@ function Base.open(core::CoreVisualizer; default_port=8700, max_retries=500, sta
# some other process grabs the given port in between the close() above
# and the open() below. But it's unlikely and would not be terribly
# damaging (the user would just have to call open() again).
return open(core, port, start_browser = start_browser)
return open(core, port; host=host, kw...)
end
end