chore(webchat): wait for local server and add debug logging
This commit is contained in:
@@ -61,6 +61,7 @@ final class WebChatServer: @unchecked Sendable {
|
||||
connection.stateUpdateHandler = { state in
|
||||
switch state {
|
||||
case .ready:
|
||||
webChatServerLogger.debug("WebChatServer connection ready")
|
||||
self.receive(on: connection)
|
||||
case let .failed(error):
|
||||
webChatServerLogger
|
||||
@@ -79,6 +80,9 @@ final class WebChatServer: @unchecked Sendable {
|
||||
self.respond(to: connection, requestData: data)
|
||||
}
|
||||
if isComplete || error != nil {
|
||||
if let error {
|
||||
webChatServerLogger.error("WebChatServer receive error: \(error.localizedDescription, privacy: .public)")
|
||||
}
|
||||
connection.cancel()
|
||||
} else {
|
||||
self.receive(on: connection)
|
||||
@@ -104,6 +108,7 @@ final class WebChatServer: @unchecked Sendable {
|
||||
if path.hasPrefix("webchat/") {
|
||||
path = String(path.dropFirst("webchat/".count))
|
||||
}
|
||||
webChatServerLogger.debug("WebChatServer request path=\(path, privacy: .public)")
|
||||
if path.isEmpty { path = "index.html" }
|
||||
|
||||
guard let root else {
|
||||
|
||||
@@ -105,7 +105,7 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler,
|
||||
self.webView.configuration.userContentController.addUserScript(userScript)
|
||||
|
||||
WebChatServer.shared.start(root: webChatURL)
|
||||
guard let baseURL = WebChatServer.shared.baseURL() else {
|
||||
guard let baseURL = self.waitForWebChatServer() else {
|
||||
webChatLogger.error("WebChatServer not ready; cannot load web chat")
|
||||
return
|
||||
}
|
||||
@@ -114,6 +114,22 @@ final class WebChatWindowController: NSWindowController, WKScriptMessageHandler,
|
||||
webChatLogger.debug("loadPage queued HTML into WKWebView url=\(url.absoluteString, privacy: .public)")
|
||||
}
|
||||
|
||||
private func waitForWebChatServer(timeout: TimeInterval = 2.0) -> URL? {
|
||||
let deadline = Date().addingTimeInterval(timeout)
|
||||
var base: URL?
|
||||
while Date() < deadline {
|
||||
if let url = WebChatServer.shared.baseURL() {
|
||||
base = url
|
||||
break
|
||||
}
|
||||
RunLoop.current.run(mode: .default, before: Date().addingTimeInterval(0.05))
|
||||
}
|
||||
if base == nil {
|
||||
webChatLogger.error("WebChatServer failed to become ready within \(timeout, privacy: .public)s")
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
webChatLogger.debug("didFinish navigation url=\(webView.url?.absoluteString ?? "nil", privacy: .public)")
|
||||
webView.evaluateJavaScript("document.body.innerText") { result, error in
|
||||
|
||||
Reference in New Issue
Block a user