Skip to content

Commit 3ff6906

Browse files
committed
update websocket uri to use wss if https is in the Server.public_url
1 parent 433be4d commit 3ff6906

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/textual_serve/server.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
from __future__ import annotations
22

33
import asyncio
4-
54
import logging
65
import os
7-
from pathlib import Path
86
import signal
97
import sys
10-
8+
from importlib.metadata import version
9+
from pathlib import Path
1110
from typing import Any
1211

1312
import aiohttp_jinja2
14-
from aiohttp import web
15-
from aiohttp import WSMsgType
16-
from aiohttp.web_runner import GracefulExit
1713
import jinja2
18-
19-
from importlib.metadata import version
20-
14+
from aiohttp import WSMsgType, web
15+
from aiohttp.web_runner import GracefulExit
2116
from rich import print
2217
from rich.console import Console
23-
from rich.logging import RichHandler
2418
from rich.highlighter import RegexHighlighter
19+
from rich.logging import RichHandler
2520

2621
from .app_service import AppService
2722

@@ -30,7 +25,9 @@
3025
LOGO = r"""[bold magenta]___ ____ _ _ ___ _ _ ____ _ ____ ____ ____ _ _ ____
3126
| |___ \/ | | | |__| | __ [__ |___ |__/ | | |___
3227
| |___ _/\_ | |__| | | |___ ___] |___ | \ \/ |___ [not bold]VVVVV
33-
""".replace("VVVVV", f"v{version('textual-serve')}")
28+
""".replace(
29+
"VVVVV", f"v{version('textual-serve')}"
30+
)
3431

3532

3633
WINDOWS = sys.platform == "WINDOWS"
@@ -92,6 +89,8 @@ def __init__(
9289
if public_url is None:
9390
if self.port == 80:
9491
self.public_url = f"http://{self.host}"
92+
elif self.port == 443:
93+
self.public_url = f"https://{self.host}"
9594
else:
9695
self.public_url = f"http://{self.host}:{self.port}"
9796
else:
@@ -214,7 +213,11 @@ def get_url(route: str, **args) -> str:
214213
def get_websocket_url(route: str, **args) -> str:
215214
"""Get a URL with a websocket prefix."""
216215
url = get_url(route, **args)
217-
return "ws:" + url.split(":", 1)[1]
216+
217+
if self.public_url.startswith("https"):
218+
return "wss:" + url.split(":", 1)[1]
219+
else:
220+
return "ws:" + url.split(":", 1)[1]
218221

219222
context = {
220223
"font_size": font_size,

0 commit comments

Comments
 (0)