launchdarkly / swift-eventsource

Server-sent events (SSE) client implementation in Swift for iOS, macOS, tvOS, and watchOS
https://launchdarkly.github.io/swift-eventsource/
Other
187 stars 34 forks source link

[LDEventSource] Connection unexpectedly closed. #69

Closed wuqinqiang closed 11 months ago

wuqinqiang commented 11 months ago

Describe the bug [LDEventSource] Connection unexpectedly closed.

To reproduce

my code


import Foundation

import LDSwiftEventSource

class MyHandler: EventHandler {
    func onOpened() {
        print("MyHandler onOpened")
    }

    func onClosed() {
        print("MyHandler onClosed")
    }

    func onMessage(eventType: String, messageEvent: MessageEvent) {
        print("MyHandler eventType:\(eventType),messageEvent:\(messageEvent.data)")
    }

    func onComment(comment: String) {
        print("MyHandler onComment:\(comment)")
    }

    func onError(error: Error) {
        print("MyHandler onError:\(error)")
    }
}

class EssayStream {
    var eventSource: EventSource?

    func GenerateEssayStream2(words:[String]) {

        let essayReq = EssayReq(words: words)

        guard let sseURL = URL(string: "http://127.0.0.1:8888/v1/word/generate_essay_stream") else {
                print("Invalid SSE URL")
                return
            }

        let myHandler = MyHandler()

        var config = EventSource.Config(handler: myHandler, url: sseURL)
        config.method = "POST"
        config.headers["Content-Type"] = "application/json"

        do {
            let jsonData = try JSONEncoder().encode(essayReq)
            config.body = jsonData
        } catch {
            print("Error encoding request data: \(error)")
            return
        }
        self.eventSource = EventSource(config: config)

        self.eventSource?.start()

    }

}

I noticed the error: Connection unexpectedly closed. but when I captured the packages, I saw a complete request and response net.csv

Expected behavior Print the content of each event from server side func onMessage(eventType: String, messageEvent: MessageEvent) { print("MyHandler eventType:(eventType),messageEvent:(messageEvent.data)") }

Logs

2023-10-05 14:53:22.588493+0800 sweet[16290:10957563] [LDEventSource] State: raw -> connecting
2023-10-05 14:53:22.589071+0800 sweet[16290:10957563] [LDEventSource] Starting EventSource client
2023-10-05 14:53:23.428033+0800 sweet[16290:10957643] [LDEventSource] Initial reply received
2023-10-05 14:53:23.428219+0800 sweet[16290:10957643] [LDEventSource] State: connecting -> open
MyHandler onOpened
2023-10-05 14:53:27.133741+0800 sweet[16290:10957563] [LDEventSource] Connection unexpectedly closed.
MyHandler onClosed
2023-10-05 14:53:27.133974+0800 sweet[16290:10957563] [LDEventSource] State: open -> closed
2023-10-05 14:53:27.134163+0800 sweet[16290:10957563] [LDEventSource] Waiting 1.957 seconds before reconnecting...
2023-10-05 14:53:29.220017+0800 sweet[16290:10957553] [LDEventSource] Starting EventSource client
2023-10-05 14:53:29.919622+0800 sweet[16290:10957643] [LDEventSource] Initial reply received
2023-10-05 14:53:29.919780+0800 sweet[16290:10957643] [LDEventSource] State: closed -> open
MyHandler onOpened

Library version 3.1.1 XCode and Swift version For instance, XCode 14.3.1

Platform the issue occurs on iPhone

Additional context

tanderson-ld commented 11 months ago

Hi @wuqinqiang , have you confirmed the behavior of the server you are connecting to is not causing the issue? This library treats a termination by the server as unexpected, so that may be what is happening here. Another way to test it yourself could be connecting to example SSE services that I'm sure are hosted around the internet for developer purposes. Let us know!

wuqinqiang commented 11 months ago

It's my fault, thank you for your reply~