Tweak URL printout in terminal when launching the web viewer.
PiperOrigin-RevId: 958422471 Change-Id: I2d637c615015915eb01b65bb0bad22749886a372
This commit is contained in:
committed by
Copybara-Service
parent
d33f297b56
commit
b0c3486249
@@ -187,7 +187,8 @@ limitations under the License.
|
|||||||
// The session id tells the server which page is dropping; it only
|
// The session id tells the server which page is dropping; it only
|
||||||
// accepts drops from the controller.
|
// accepts drops from the controller.
|
||||||
const sid = (window.Module && Module.sessionId) || "";
|
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.binaryType = "arraybuffer";
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
for (const frame of frames) ws.send(frame);
|
for (const frame of frames) ws.send(frame);
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ void RemoteUi::Connect(const std::string& url) {
|
|||||||
Network::Disconnect(socket_);
|
Network::Disconnect(socket_);
|
||||||
socket_ = nullptr;
|
socket_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The port argument is unused when a full URL is passed.
|
// The port argument is unused when a full URL is passed.
|
||||||
LOG(Info, "Connecting to WebSocket at %s", url.c_str());
|
LOG(Info, "Connecting to WebSocket at %s", url.c_str());
|
||||||
socket_ = Network::Connect(url.c_str(), 0);
|
socket_ = Network::Connect(url.c_str(), 0);
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ void Session::CloseSocket() {
|
|||||||
void Session::Connect(const std::string& url) {
|
void Session::Connect(const std::string& url) {
|
||||||
// Drop any lingering socket first so a reconnect cannot leak the old one.
|
// Drop any lingering socket first so a reconnect cannot leak the old one.
|
||||||
CloseSocket();
|
CloseSocket();
|
||||||
|
|
||||||
EmscriptenWebSocketCreateAttributes attr;
|
EmscriptenWebSocketCreateAttributes attr;
|
||||||
emscripten_websocket_init_create_attributes(&attr);
|
emscripten_websocket_init_create_attributes(&attr);
|
||||||
attr.url = url.c_str();
|
attr.url = url.c_str();
|
||||||
|
|||||||
@@ -70,60 +70,35 @@ def _find_assets_dir() -> str:
|
|||||||
return ''
|
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:
|
def _print_url_banner(host: str, port: int) -> None:
|
||||||
"""Prints a prominent boxed banner with the URLs browsers can use."""
|
"""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'):
|
if host in ('::', '0.0.0.0'):
|
||||||
# Shareable with other machines on the same network. Both families are
|
fqdn = socket.getfqdn()
|
||||||
# always listed: a visitor may only be reachable over one of them, and an
|
rows.append(('Remote:', f'http://{fqdn}:{port}/'))
|
||||||
# explicit "(unavailable)" beats a silently missing row. IPv6 literals must
|
rows.append(('Local:', f'http://localhost:{port}/'))
|
||||||
# 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)')
|
|
||||||
)
|
|
||||||
|
|
||||||
label_width = max(len(label) for label, _ in rows)
|
label_width = max(len(label) for label, _ in rows)
|
||||||
lines = ['MuJoCo Web Viewer running at:', '']
|
lines_plain = ['MuJoCo Web Viewer running at:', '']
|
||||||
lines += [f' {label.ljust(label_width)} {url}' for label, url in rows]
|
lines_plain += [f' {label.ljust(label_width)} {url}' for label, url in rows]
|
||||||
lines += ['', 'Ctrl+C to quit']
|
lines_plain += ['', 'Ctrl+C to quit']
|
||||||
|
|
||||||
width = max(len(line) for line in lines)
|
width = max(len(line) for line in lines_plain)
|
||||||
banner = [
|
|
||||||
'+' + '-' * (width + 2) + '+',
|
def _hyperlink(url: str) -> str:
|
||||||
*(f'| {line.ljust(width)} |' for line in lines),
|
return f'\033]8;;{url}\033\\{url}\033]8;;\033\\'
|
||||||
'+' + '-' * (width + 2) + '+',
|
|
||||||
|
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)
|
print('\n'.join(banner), flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user