Working example.

The **only problem** , the `request.close()` seems to not actually close the 
socket. The socket will be closed only when the nim process is killed. Is there 
a way to forcefully close the socket?
    
    
    import jester, httpcore, asyncdispatch, strformat
    
    routes:
      get "/demo_sse":
        enableRawMode()
        request.sendHeaders(HttpCode(200), @[
          ("Content-Type", "text/event-stream"),
          ("Cache-Control", "no-cache"),
          ("Connection", "keep-alive"),
          ("Access-Control-Allow-Origin", "*")
        ])
        # res.write("retry: 10000\n\n");
        for i in 1..5:
          request.send("data: " & $i & "\n\n")
          await sleep_async(1000)
        resp "data: some-last-message\n\n"
        request.close() # <== problem here, the socket is not closed
    
    
    Run
    
    
    var source = new EventSource("http://localhost:5000/demo_sse";)
    source.onmessage = function (event) {
      console.log(event)
    }
    source.onerror = function (event) {
      console.log("closed")
    }
    source.onopen = function (event) {
      console.log("opened")
    }
    
    
    Run

Reply via email to