Tweak URL printout in terminal when launching the web viewer.

PiperOrigin-RevId: 958422471
Change-Id: I2d637c615015915eb01b65bb0bad22749886a372
This commit is contained in:
Saran Tunyasuvunakool
2026-08-03 09:40:24 -07:00
committed by Copybara-Service
parent d33f297b56
commit b0c3486249
4 changed files with 26 additions and 48 deletions
@@ -187,7 +187,8 @@ limitations under the License.
// The session id tells the server which page is dropping; it only
// accepts drops from the controller.
const sid = (window.Module && Module.sessionId) || "";
const ws = new WebSocket(proto + location.host + "/drop?sid=" + encodeURIComponent(sid));
const dropUrl = proto + location.host + "/drop?sid=" + encodeURIComponent(sid);
const ws = new WebSocket(dropUrl);
ws.binaryType = "arraybuffer";
ws.onopen = () => {
for (const frame of frames) ws.send(frame);
@@ -72,6 +72,7 @@ void RemoteUi::Connect(const std::string& url) {
Network::Disconnect(socket_);
socket_ = nullptr;
}
// The port argument is unused when a full URL is passed.
LOG(Info, "Connecting to WebSocket at %s", url.c_str());
socket_ = Network::Connect(url.c_str(), 0);
@@ -122,6 +122,7 @@ void Session::CloseSocket() {
void Session::Connect(const std::string& url) {
// Drop any lingering socket first so a reconnect cannot leak the old one.
CloseSocket();
EmscriptenWebSocketCreateAttributes attr;
emscripten_websocket_init_create_attributes(&attr);
attr.url = url.c_str();
+22 -47
View File
@@ -70,60 +70,35 @@ def _find_assets_dir() -> str:
return ''
def _lan_ips() -> tuple[str | None, str | None]:
"""Returns this machine's outbound-interface (IPv6, IPv4) addresses.
These are the addresses other machines on the same network can reach the
viewer at. A None entry means that family has no shareable address.
"""
def probe(
family: int | socket.AddressFamily, dest: tuple[str, int]
) -> str | None:
try:
with socket.socket(family, socket.SOCK_DGRAM) as s:
s.connect(dest)
return s.getsockname()[0]
except OSError:
return None
ipv6 = probe(socket.AF_INET6, ('2001:4860:4860::8888', 80)) # pytype: disable=wrong-arg-types
if ipv6 and ipv6.startswith(('fe80', '::1')):
ipv6 = None
ipv4 = probe(socket.AF_INET, ('8.8.8.8', 80)) # pytype: disable=wrong-arg-types
if ipv4 and ipv4.startswith('127.'):
ipv4 = None
return ipv6, ipv4
def _print_url_banner(host: str, port: int) -> None:
"""Prints a prominent boxed banner with the URLs browsers can use."""
rows = [('local', f'http://localhost:{port}')]
rows = []
if host in ('::', '0.0.0.0'):
# Shareable with other machines on the same network. Both families are
# always listed: a visitor may only be reachable over one of them, and an
# explicit "(unavailable)" beats a silently missing row. IPv6 literals must
# be bracketed in URLs.
ipv6, ipv4 = _lan_ips()
rows.append((
'network (IPv6)',
f'http://[{ipv6}]:{port}' if ipv6 else '(unavailable)',
))
rows.append(
('network (IPv4)', f'http://{ipv4}:{port}' if ipv4 else '(unavailable)')
)
fqdn = socket.getfqdn()
rows.append(('Remote:', f'http://{fqdn}:{port}/'))
rows.append(('Local:', f'http://localhost:{port}/'))
label_width = max(len(label) for label, _ in rows)
lines = ['MuJoCo Web Viewer running at:', '']
lines += [f' {label.ljust(label_width)} {url}' for label, url in rows]
lines += ['', 'Ctrl+C to quit']
lines_plain = ['MuJoCo Web Viewer running at:', '']
lines_plain += [f' {label.ljust(label_width)} {url}' for label, url in rows]
lines_plain += ['', 'Ctrl+C to quit']
width = max(len(line) for line in lines)
banner = [
'+' + '-' * (width + 2) + '+',
*(f'| {line.ljust(width)} |' for line in lines),
'+' + '-' * (width + 2) + '+',
width = max(len(line) for line in lines_plain)
def _hyperlink(url: str) -> str:
return f'\033]8;;{url}\033\\{url}\033]8;;\033\\'
lines_formatted = ['MuJoCo Web Viewer running at:', '']
lines_formatted += [
f' {label.ljust(label_width)} {_hyperlink(url)}' for label, url in rows
]
lines_formatted += ['', 'Ctrl+C to quit']
banner = ['+' + '-' * (width + 2) + '+']
for plain, formatted in zip(lines_plain, lines_formatted):
padding = ' ' * (width - len(plain))
banner.append(f'| {formatted}{padding} |')
banner.append('+' + '-' * (width + 2) + '+')
print('\n'.join(banner), flush=True)