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

Connection Error 🐛 #64

Closed jeongshin closed 1 year ago

jeongshin commented 1 year ago

Describe the bug

Hello thanks for great library 🙌. I'm trying to build wrapper library using LDSwiftEventSource internally on my react-native app. However, I'm having problem with getting any events from server.

Logs are attached as screen shots below.

My questions are

  1. Is this library issue or am I doing anything wrong?
  2. Can I get callback for error below?

To reproduce Here's my minimal code

import LDSwiftEventSource

@objc(EventSource)
class EventSource: NSObject {
    private var connceted = false;

    @objc(connect:options:)
    func connect(url: String, options: NSObject) -> Void {
        print(url, options, self.connceted);

        let handlers = MyEventHandler()

        guard let url = URL(string: url) else {
            fatalError("Invalid URL")
        }

        var config = LDSwiftEventSource.EventSource.Config(handler: handlers, url: url);

        config.connectionErrorHandler = self.connectionErrorHandler

        config.method = "GET"

        config.headers = ["Authorization": "REQUIRED TOKEN", "Content-Type": "application/json", "Accept": "text/event-source"]

        var instance = LDSwiftEventSource.EventSource(config: config);

        instance.start();

        self.connceted = true;

        print("started instance")
    }

    @objc(disconnect)
    func disconnect() -> Void {
        print("disconnected")
    }

    func connectionErrorHandler(error: Error) -> LDSwiftEventSource.ConnectionErrorAction {
        print("RNES error", error)
        guard let unsuccessfulResponseError = error as? UnsuccessfulResponseError else { return .proceed }

        let responseCode: Int = unsuccessfulResponseError.responseCode
        if 204 == responseCode {
            return .shutdown
        }
        return .proceed
    }
}

public class MyEventHandler: LDSwiftEventSource.EventHandler {
    public func onOpened() {
        print("RNES Connection opened")
    }

    public func onClosed() {
        print("RNES Connection closed")
    }

    public func onMessage(eventType: String, messageEvent: MessageEvent) {
        print("RNES Received event of type \(eventType) with data \(messageEvent)")
    }

    public func onComment(comment: String) {
        print("RNES Received comment: \(comment)")
    }

    public func onError(error: Error) {
        print("RNES Error occurred: \(error)")
    }
}

Expected behavior I'm expecting to get any data from server. I'm mocking local server with express using ngrok to test on real device. But I can't get any request from client on my server console. I'm using same server code and works fine on browser & android.

Logs

Here's my logs.

Screenshot 2023-09-13 at 3 43 55 PM

Library version "LDSwiftEventSource": "3.1.1"

XCode and Swift version XCode 14.3.1, Swift 5.8.1

Platform the issue occurs on Testing on iPhone real device & iPhone simulator.

jeongshin commented 1 year ago

Solved issue by following example code from https://github.com/launchdarkly/swift-eventsource/issues/53 Thanks!!