mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
fix: use baseUrl for Socket.IO connection to support separate frontend hosting
Previously, useTerminalSocket used a relative path `/terminal` for Socket.IO, which would connect to the frontend host instead of the actual HAPI server. Now it uses the baseUrl from context to connect to the correct server, enabling scenarios like hosting the frontend on GitHub Pages while the server runs elsewhere.
This commit is contained in:
@@ -8,6 +8,7 @@ type TerminalConnectionState =
|
||||
| { status: 'error'; error: string }
|
||||
|
||||
type UseTerminalSocketOptions = {
|
||||
baseUrl: string
|
||||
token: string
|
||||
sessionId: string
|
||||
terminalId: string
|
||||
@@ -49,12 +50,14 @@ export function useTerminalSocket(options: UseTerminalSocketOptions): {
|
||||
const sessionIdRef = useRef(options.sessionId)
|
||||
const terminalIdRef = useRef(options.terminalId)
|
||||
const tokenRef = useRef(options.token)
|
||||
const baseUrlRef = useRef(options.baseUrl)
|
||||
const lastSizeRef = useRef<{ cols: number; rows: number } | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
sessionIdRef.current = options.sessionId
|
||||
terminalIdRef.current = options.terminalId
|
||||
}, [options.sessionId, options.terminalId])
|
||||
baseUrlRef.current = options.baseUrl
|
||||
}, [options.sessionId, options.terminalId, options.baseUrl])
|
||||
|
||||
useEffect(() => {
|
||||
tokenRef.current = options.token
|
||||
@@ -113,7 +116,7 @@ export function useTerminalSocket(options: UseTerminalSocketOptions): {
|
||||
return
|
||||
}
|
||||
|
||||
const socket = io('/terminal', {
|
||||
const socket = io(`${baseUrlRef.current}/terminal`, {
|
||||
auth: { token },
|
||||
path: '/socket.io/',
|
||||
reconnection: true,
|
||||
|
||||
Reference in New Issue
Block a user