wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
25.25k stars 1.22k forks source link

I made a mistake when I used EventSource(SSE) to push data forward #2847

Open crismcn opened 1 year ago

crismcn commented 1 year ago

Description

TEST Code:

backend

err := wails.Run(&options.App{
        Title:  "YYF-WAILS-DEPLOY",
        Width:  1024,
        Height: 768,
        AssetServer: &assetserver.Options{
            Assets:  assets,
            Handler: web.HandlerByGin(), // Do I need to configure anything in the code

        },
                ...
}

func DeployToServer(c *gin.Context) {
    c.Header("Content-Type", "text/event-stream")
    c.Header("Cache-Control", "no-cache")
    c.Header("Connection", "keep-alive")
    c.Header("Access-Control-Allow-Origin", "*")
    for {
        c.SSEvent("message", "data: This is a SSE event \n\n")
        c.Writer.Flush()  //  -----> This line gives an error
        time.Sleep(1 * time.Second)
    }
}

JS:

const eventSource = new EventSource("/api/v1/deploy/to-server");
eventSource.onmessage = function(event) {
  var eventData = event.data;
  console.log("Received event data:", eventData);
};

eventSource.onerror = (e: any) => {
  console.error("An error occurred: readyState=" + e.target.readyState + "status=" + e.target.status);
  console.error("Server response:", e.target.responseText);
};

Response:

{
  "code":500,
  "data":null,
  "msg":"interface conversion: *assetserver.contentTypeSniffer is not http.Flusher: missing method Flush"
}

To Reproduce

no

Expected behaviour

no

Screenshots

![Uploading image.png…]()

Attempted Fixes

No response

System Details

so about The AssetServer Do I need to do any configuration?

Additional context

No response

langhuihui commented 1 year ago

Blocking occurs in the Windows operating system when using SSE

crismcn commented 1 year ago

Blocking occurs in the Windows operating system when using SSE

Windows会阻塞吗,可是我用Chrome直接调用接口好像是正常的,我看报错说的是返回类型不支持;怀疑是Wails AssetServer的问题导致的

langhuihui commented 1 year ago

我主要是做了一层转发,另外起了一个 server,结果 sse 需要保持连接,就把 UI 阻塞了,mac 下是正常的,只有 windows 有问题

crismcn commented 1 year ago

我主要是做了一层转发,另外起了一个 server,结果 sse 需要保持连接,就把 UI 阻塞了,mac 下是正常的,只有 windows 有问题 我把js调用go的逻辑,改成Gin REST API的方式调用,也遇到了UI阻塞的问题;

zwjjiaozhu commented 9 months ago

我主要是做了一层转发,另外起了一个 server,结果 sse 需要保持连接,就把 UI 阻塞了,mac 下是正常的,只有 windows 有问题 我把js调用go的逻辑,改成Gin REST API的方式调用,也遇到了UI阻塞的问题;

请问你后来是咋解决的

langhuihui commented 9 months ago

@zwjjiaozhu 换了 tauri

zwjjiaozhu commented 9 months ago

@zwjjiaozhu 换了 tauri

哈哈哈👍

zwjjiaozhu commented 9 months ago

我也遇到用自带assetsServer转发前端SSE,发生阻塞的问题,其原因就是后端的数据没有实时 flush 到客户端中。 题主说的:c.Writer.Flush() 会报错。我看了下源码,发现wails的writer中没有实现flush的方法。

我的解决办法: 后端另外开启了一个 http 服务(127.0.0.1:7743),给前端提供sse转发服务。 可能会遇到 跨域请求的错误,只需要后端

w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
    w.Header().Add("Access-Control-Allow-Headers", "*")

前端需要在请求的时候,指定跨域规则,如:

fetch(url, {
    mode: "cors",
    ...
})

不过我前端SSE用的是:@microsoft/fetch-event-source 原因是可以发送post请求,并可以携带body、header等字段

fetchEventSource(newUrl, {
        method: "POST",
        headers: header,
        retry: false,
        mode: "cors",
...
}
)

这样就可以完美实现后端转发SSE请求。