Razvan
07/04/2021, 5:27 PMlogs.html:1 Access to resource at '<http://localhost:8080/logsse>' from origin '<http://localhost:8880>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Couldn't figure out how to set that header...dave
07/04/2021, 6:11 PMRazvan
07/05/2021, 5:45 AMRazvan
07/05/2021, 5:49 AMdave
07/05/2021, 12:58 PMRazvan
07/05/2021, 5:18 PMRazvan
07/05/2021, 5:48 PMfun main() {
val client = routes(
"/test" bind Method.GET to { req: Request ->
Response(Status.OK)
.header("content-type", ContentType.TEXT_HTML.value)
.body(html())
}
)
val serversse = sse(
"/hi" bind { sse: Sse ->
sse.send(SseMessage.Data("Hello to you"))
}
)
val serverWithSse = PolyHandler(client, sse = serversse)
.asServer(Undertow(8080)).start()
val serverClient = client.asServer(Undertow(8880)).start()
}
fun html() = """
<html>
<head>
<script>
const SSE_URL = `<http://localhost:8080/hi>`;
let evtSrc = new EventSource(SSE_URL);
evtSrc.onmessage = (event) => { console.log(event.data)
document.getElementById("msg").innerText = event.data
}
evtSrc.onopen = (event) => console.log("SSE Opened")
evtSrc.onerror = (event) => console.log("SSE error")
</script>
</head>
<body>
<h3>Last message</h3>
<div id="msg">(no msg received)</div>
</body>
</html>
""".trimIndent()
Razvan
07/05/2021, 5:50 PM<http://localhost:8080/test>
you will see no error and "hello to you" as last message
but if you go to <http://localhost:8880/test>
you don;t have the message and if you check the browser console you'll see the error:
Access to resource at '<http://localhost:8080/hi>' from origin '<http://localhost:8880>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
dave
07/05/2021, 5:52 PMdave
07/05/2021, 5:53 PMdave
07/05/2021, 5:53 PMRazvan
07/05/2021, 5:56 PMdave
07/05/2021, 5:57 PMRazvan
07/05/2021, 5:57 PMdave
07/05/2021, 5:59 PMRazvan
07/05/2021, 6:00 PMRazvan
07/05/2021, 6:00 PMdave
07/05/2021, 6:02 PMRazvan
07/05/2021, 6:08 PMRazvan
07/05/2021, 6:15 PMdave
07/05/2021, 6:19 PMdave
07/05/2021, 6:19 PMRazvan
07/05/2021, 6:41 PMHttp4kUndertowSseCallback
the connected function receive as param a ServerSentEventConnection
and that object has a getResponseHeaders
i guess is not read only, although but the comment is a bit confusing
Returns:
The response headers from the initial request that opened this connection
dave
07/05/2021, 6:41 PMRazvan
07/05/2021, 6:42 PMdave
07/05/2021, 6:42 PMdave
07/05/2021, 6:47 PMRazvan
07/05/2021, 7:02 PMexchange
is the key but you can't seem to access it directly and if getResponseHeaders is too late, I can't see how htt4k's adapter could set it... and don't count on undertow.io documentationdave
07/05/2021, 7:03 PMdave
07/05/2021, 7:03 PMRazvan
07/05/2021, 8:01 PMdave
07/05/2021, 8:02 PMRazvan
07/06/2021, 9:58 AMval serversse = sse(
"/hi" bind { sse: Sse ->
println(sse.connectRequest)
sse.send(SseMessage.Data("Hello to you"))
println("Data sent")
}
)
the lines are well printed on each call, I guess if it was the server that blocked it shouldn't allow to run the heandler for nothing.
also logging the connectRequest
I see in the request Sec-Fetch-Site: same-site
so is it the JS that only accept connection to the same site ? couldn't find any info on how I could change that...
Well I guess I should try a JS SSE server to check that... 🙂 Anyway, just to share the last updates.Razvan
07/06/2021, 12:46 PMRazvan
07/06/2021, 4:46 PMdave
07/07/2021, 9:10 AM