Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to set http body content #164

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions EventSource/EventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand All @@ -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() {
Expand All @@ -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
}

Expand Down Expand Up @@ -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] {
Expand All @@ -252,3 +252,11 @@ private extension EventSource {
}
}
}

public extension EventSourceProtocol {
var retryTime: Int {
return 5
}
}


14 changes: 14 additions & 0 deletions EventSourceHttpBody.podspec
Original file line number Diff line number Diff line change
@@ -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' => '[email protected]' }
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
9 changes: 7 additions & 2 deletions EventSourceSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions IKEventSource.podspec

This file was deleted.