Some message can't call closed callback
Description This is probably the best SSE implementation for Swift I have found on Github. But there is a problem with consistency.
To Reproduce
The sse server transmits a piece of data as follows: {"id":0, "status":"auth", "msg":""},
let eventSource = EventSource(mode: .dataOnly) dataTask = await eventSource.dataTask(for: urlRequest) for await event in await dataTask!.events() {}
event cannot correctly obtain the closed callback, please help to check, thank you
Expected behavior After any message transmission is completed, you can get the closed callback
- Platform: iOS 18.1.1
- EventSource version: 0.1.4]
Additional context Thanks for this great project.
The EventType.closed event is triggered only when the connection is manually closed, the server responds with a 204 status code, an error occurs, or the parent task running the EventSource.DataTask is canceled.
Could you provide more details about the issue you're encountering?
When the transmitted content is {"id":0, "status":"name", "msg":""}, sse can correctly obtain the closed callback, and the status code of both events is 200.
for await event in await dataTask!.events() { print("\(event)")} ,each sse verification is checked by print("(event)")
I concur that this is a great SSE implementation.. but can find no way to close the connection if I want to clean up and possibly open up again later...
You mention in the Usage section to use dataTask.cancel() to close the connection... but its wanting a parameter and no clue how to actually cancel or close the stream ... Please advise.
Yeah, the method mentioned in the README is outdated. I need to update it.
For now, you can close the connection by cancelling the parent Task:
let task = Task {
let eventSource = EventSource()
let dataTask = eventSource.dataTask(for: urlRequest)
for await event in dataTask.events() {
...
}
}
task.cancel()
When I try and call that it's requiring a parameter of urlSession .... currently I'm trying to call that as follows:
Oh crap.. I misread and was trying to cancel the wrong thing...
But I have to ask still if canceling the task itself will stop the async streams and close them up nicely? or at least as nice as possible...
Yes, the events stream supports cooperative cancellation. Internally, it closes the connection when the parent Task is cancelled and calls the aforementioned cancel(urlSession:) method with the right URLSession instance that was used by EventSource
Thank you so much. I really appreciate your work here. It was wonderful to find an SSE client that works great with Swift Language set to Swift 6 in xCode. You have done a wonderful job with this.
The only problem I had was using it to listen for SSE events on the Philips Hue bridges. They are SSL only and I had to take the code and add in the URL session challenge to basically ignore the challenge .. They self signed their certificate so I'm not seeing how trusted that can be. At some point I may try and actually add the certificate into the mix and come up with a way to pass that to EventSource and have the challenge actually work with a certificate. If I get to that point I'll send you the modifications I have to make.. but right now I've got more pressing things to address in my project.
Thank you again for sharing this with us.
Sorry, I used return incorrectly, which caused the event not to execute normally. Thank you for your answer, this question can be closed.