From 24166873630f5fa84f9be45259d196697bf1ef72 Mon Sep 17 00:00:00 2001 From: lashlyn Date: Fri, 3 May 2024 15:42:45 +0700 Subject: [PATCH 1/2] added ability to set http body content --- EventSource/EventSource.swift | 38 ++++++++++++++++---------- EventSourceSample/ViewController.swift | 9 ++++-- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/EventSource/EventSource.swift b/EventSource/EventSource.swift index 8249968..cb80ee0 100644 --- a/EventSource/EventSource.swift +++ b/EventSource/EventSource.swift @@ -76,7 +76,8 @@ public protocol EventSourceProtocol { open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate { static let DefaultRetryTime = 3000 - public let url: URL + public let urlRequest: URLRequest + public var url: URL { urlRequest.url! } private(set) public var lastEventId: String? private(set) public var retryTime = EventSource.DefaultRetryTime private(set) public var headers: [String: String] @@ -93,11 +94,10 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate { private var urlSession: URLSession? public init( - url: URL, - headers: [String: String] = [:] + urlRequest: URLRequest ) { - self.url = url - self.headers = headers + self.urlRequest = urlRequest + self.headers = urlRequest.allHTTPHeaderFields ?? [:] readyState = EventSourceState.closed operationQueue = OperationQueue() @@ -112,7 +112,7 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate { let configuration = sessionConfiguration(lastEventId: lastEventId) urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue) - urlSession?.dataTask(with: url).resume() + urlSession?.dataTask(with: urlRequest).resume() } public func disconnect() { @@ -137,17 +137,17 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate { eventListeners[event] = handler } - public func removeEventListener(_ event: String) { - eventListeners.removeValue(forKey: event) - } + public func removeEventListener(_ event: String) { + eventListeners.removeValue(forKey: event) + } - public func events() -> [String] { - return Array(eventListeners.keys) - } + public func events() -> [String] { + return Array(eventListeners.keys) + } open func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { - if readyState != .open { + if readyState != .open { return } @@ -229,8 +229,8 @@ private extension EventSource { continue } - if event.event == nil || event.event == "message" { - mainQueue.async { [weak self] in self?.onMessageCallback?(event.id, "message", event.data) } + if event.event == nil || event.event == "thread.message.delta" { + mainQueue.async { [weak self] in self?.onMessageCallback?(event.id, "thread.message.delta", event.data) } } if let eventName = event.event, let eventHandler = eventListeners[eventName] { @@ -252,3 +252,11 @@ private extension EventSource { } } } + +public extension EventSourceProtocol { + var retryTime: Int { + return 5 + } +} + + diff --git a/EventSourceSample/ViewController.swift b/EventSourceSample/ViewController.swift index 6c7325a..da43755 100644 --- a/EventSourceSample/ViewController.swift +++ b/EventSourceSample/ViewController.swift @@ -19,8 +19,13 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let serverURL = URL(string: "http://127.0.0.1:8080/sse")! - eventSource = EventSource(url: serverURL, headers: ["Authorization": "Bearer basic-auth-token"]) + var urlRequest = URLRequest(url: URL(string: "http://127.0.0.1:8080/sse")!) + urlRequest.httpMethod = "POST" + urlRequest.addValue("Bearer basic-auth-token", forHTTPHeaderField: "Authorization") + + eventSource = EventSource(urlRequest: urlRequest) + + eventSource?.connect() eventSource?.onOpen { [weak self] in self?.status.backgroundColor = UIColor(red: 166/255, green: 226/255, blue: 46/255, alpha: 1) From bca41e8d3a10dd0c107ae6fc8adb7a821d7cfda0 Mon Sep 17 00:00:00 2001 From: lashlyn Date: Wed, 8 May 2024 19:04:38 +0700 Subject: [PATCH 2/2] update podspec --- EventSourceHttpBody.podspec | 14 ++++++++++++++ IKEventSource.podspec | 16 ---------------- 2 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 EventSourceHttpBody.podspec delete mode 100644 IKEventSource.podspec diff --git a/EventSourceHttpBody.podspec b/EventSourceHttpBody.podspec new file mode 100644 index 0000000..acfb2f4 --- /dev/null +++ b/EventSourceHttpBody.podspec @@ -0,0 +1,14 @@ + +Pod::Spec.new do |s| + s.name = "EventSourceHttpBody" + s.version = "1.0.0" + s.summary = "Forked from https://github.com/inaka/EventSource with the added ability to include HttpBody" + s.homepage = "https://github.com/exyte/EventSource" + s.screenshots = "http://giant.gfycat.com/BossyDistantHadrosaurus.gif" + s.license = 'MIT' + s.author = { 'Exyte' => 'info@exyte.com' } + s.social_media_url = 'http://exyte.com' + s.ios.deployment_target = '10.0' + s.source = { :git => "https://github.com/exyte/EventSource.git" } + s.source_files = "EventSource/*.swift" +end diff --git a/IKEventSource.podspec b/IKEventSource.podspec deleted file mode 100644 index 3e927ea..0000000 --- a/IKEventSource.podspec +++ /dev/null @@ -1,16 +0,0 @@ - -Pod::Spec.new do |s| - s.name = "IKEventSource" - s.version = "3.0.1" - s.summary = "Server-sent events EventSource implementation in Swift." - s.homepage = "https://github.com/inaka/EventSource" - s.screenshots = "http://giant.gfycat.com/BossyDistantHadrosaurus.gif" - s.license = "Apache License Version 2.0" - s.author = { "Andres Canal" => "andresyo990@gmail.com" } - s.social_media_url = "http://twitter.com/inaka" - s.ios.deployment_target = '8.0' - s.tvos.deployment_target = '9.0' - s.osx.deployment_target = '10.10' - s.source = { :git => "https://github.com/inaka/EventSource.git" } - s.source_files = "EventSource/*.swift" -end