Skip to content

Commit

Permalink
added methods for connection headers updating
Browse files Browse the repository at this point in the history
  • Loading branch information
a.pochtarev committed Mar 31, 2024
1 parent 37fcbeb commit e3e2601
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Sources/SwiftCentrifuge/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,29 @@ public class CentrifugeClient {
}
}

/**
update(headers:) allows updating headers before next connect.
- parameter headers: [String: String]
*/
public func update(headers: [String: String?]) {
self.syncQueue.async { [weak self] in
guard let strongSelf = self else { return }

if let conn = strongSelf.conn {
conn.update(headers: headers)
} else {
for (key, value) in headers {
guard let value else {
strongSelf.config.headers.removeValue(forKey: key)
continue
}
strongSelf.config.headers[key] = value
}
}
}
}


/**
Create subscription object to specific channel with delegate
- parameter channel: String
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftCentrifuge/WebSocket/NativeWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class NativeWebSocket: NSObject, WebSocketInterface, URLSessionWebSocketDe
private var session: URLSession?

private let log: CentrifugeLogger
private let request: URLRequest
private var request: URLRequest //this is only to allow headers, timeout, etc to be modified on reconnect
private let queue: DispatchQueue

/// The websocket is considered 'active' when `task` is not nil
Expand Down Expand Up @@ -76,6 +76,12 @@ final class NativeWebSocket: NSObject, WebSocketInterface, URLSessionWebSocketDe
})
}

func update(headers: [String : String?]) {
for (key, value) in headers {
self.request.setValue(value, forHTTPHeaderField: key)
}
}

private func doRead() {
task?.receive { [weak self] (result) in
guard let self = self else { return }
Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftCentrifuge/WebSocket/StarscreamWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ final class StarscreamWebSocket: WebSocketInterface {
socket.write(data: data)
}

func update(headers: [String : String?]) {
for (key, value) in headers {
self.socket.request.setValue(value, forHTTPHeaderField: key)
}
}

private func setup() {
socket.onConnect = { [weak self] in
self?.onConnect?()
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftCentrifuge/WebSocket/WebSocketInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ protocol WebSocketInterface: AnyObject {
var onDisconnect: ((CentrifugeDisconnectOptions, Error?) -> Void)? { get set }
var onData: ((Data) -> Void)? { get set }

func update(headers: [String: String?])

func connect()
func disconnect()
func write(data: Data)
Expand Down

0 comments on commit e3e2601

Please sign in to comment.