mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
Merge pull request #2804 from yangluxin613/feat/web-port-browser
feat(web): auto-switch port on conflict and open browser on startup
This commit is contained in:
@@ -554,6 +554,13 @@ class WebChannel(ChatChannel):
|
|||||||
logger.info(f"[WebChannel] 🌐 本地访问: http://localhost:{port}")
|
logger.info(f"[WebChannel] 🌐 本地访问: http://localhost:{port}")
|
||||||
logger.info(f"[WebChannel] 🌍 服务器访问: http://YOUR_IP:{port} (请将YOUR_IP替换为服务器IP)")
|
logger.info(f"[WebChannel] 🌍 服务器访问: http://YOUR_IP:{port} (请将YOUR_IP替换为服务器IP)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
import webbrowser
|
||||||
|
webbrowser.open(f"http://localhost:{port}")
|
||||||
|
logger.debug(f"[WebChannel] Opened browser at http://localhost:{port}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"[WebChannel] Could not open browser: {e}")
|
||||||
|
|
||||||
# 确保静态文件目录存在
|
# 确保静态文件目录存在
|
||||||
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
||||||
if not os.path.exists(static_dir):
|
if not os.path.exists(static_dir):
|
||||||
@@ -619,6 +626,13 @@ class WebChannel(ChatChannel):
|
|||||||
server.start()
|
server.start()
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
server.stop()
|
server.stop()
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno in (48, 98): # macOS/Linux EADDRINUSE
|
||||||
|
logger.error(
|
||||||
|
f"[WebChannel] 端口 {port} 已被占用,可执行 `cow restart` 清理残留进程,"
|
||||||
|
f"或在 config.json 中修改 web_port"
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
if self._http_server:
|
if self._http_server:
|
||||||
|
|||||||
Reference in New Issue
Block a user