Skip to content
Merged
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
2 changes: 1 addition & 1 deletion EasyCode.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = 'EasyCode'
s.license = 'MIT'
s.version = '1.4.1'
s.version = '1.4.2'
s.summary = 'EasyCode'
s.homepage = 'https://github.com/Salmik/EaseCode'
s.authors = { 'Salmik' => 'salmik94@gmail.com' }
Expand Down
20 changes: 20 additions & 0 deletions EasyCode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
BBCC22D92C352ADF00CC19C9 /* MailConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBCC22D82C352ADF00CC19C9 /* MailConfiguration.swift */; };
BBE498CA2D2D06930085233A /* BackgroundTaskManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBE498C92D2D06930085233A /* BackgroundTaskManager.swift */; };
BBEB8E3A2D2CFB6B00988E03 /* UnownedInject.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEB8E392D2CFB6B00988E03 /* UnownedInject.swift */; };
BBEBFB5E2D3E03D600E566A6 /* WebSocketManagerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEBFB5D2D3E03D600E566A6 /* WebSocketManagerDelegate.swift */; };
BBEBFB602D3E03E800E566A6 /* WebSocketEndPointProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEBFB5F2D3E03E800E566A6 /* WebSocketEndPointProtocol.swift */; };
BBEBFB622D3E03F500E566A6 /* WebSocketManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEBFB612D3E03F500E566A6 /* WebSocketManager.swift */; };
BBEE8D752C6B679C00133020 /* PaddedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEE8D742C6B679C00133020 /* PaddedLabel.swift */; };
BBEE8D772C6B6BB500133020 /* TipViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBEE8D762C6B6BB500133020 /* TipViewController.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -211,6 +214,9 @@
BBCC22D82C352ADF00CC19C9 /* MailConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MailConfiguration.swift; sourceTree = "<group>"; };
BBE498C92D2D06930085233A /* BackgroundTaskManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundTaskManager.swift; sourceTree = "<group>"; };
BBEB8E392D2CFB6B00988E03 /* UnownedInject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnownedInject.swift; sourceTree = "<group>"; };
BBEBFB5D2D3E03D600E566A6 /* WebSocketManagerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketManagerDelegate.swift; sourceTree = "<group>"; };
BBEBFB5F2D3E03E800E566A6 /* WebSocketEndPointProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketEndPointProtocol.swift; sourceTree = "<group>"; };
BBEBFB612D3E03F500E566A6 /* WebSocketManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebSocketManager.swift; sourceTree = "<group>"; };
BBEE8D742C6B679C00133020 /* PaddedLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddedLabel.swift; sourceTree = "<group>"; };
BBEE8D762C6B6BB500133020 /* TipViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TipViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -269,6 +275,7 @@
BB3C2EF92C6A0D3B00149867 /* Network Layer */ = {
isa = PBXGroup;
children = (
BBEBFB5C2D3E03C600E566A6 /* WebSocket */,
BBBCE96C2CFCB67E0061DB04 /* WindowLogger */,
BB3C2EFA2C6A0D5A00149867 /* NetworkResponseProtocol.swift */,
BB3C2EFC2C6A0D6C00149867 /* EndPointProtocol.swift */,
Expand Down Expand Up @@ -417,6 +424,16 @@
path = BottomSheet;
sourceTree = "<group>";
};
BBEBFB5C2D3E03C600E566A6 /* WebSocket */ = {
isa = PBXGroup;
children = (
BBEBFB5D2D3E03D600E566A6 /* WebSocketManagerDelegate.swift */,
BBEBFB5F2D3E03E800E566A6 /* WebSocketEndPointProtocol.swift */,
BBEBFB612D3E03F500E566A6 /* WebSocketManager.swift */,
);
path = WebSocket;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -596,6 +613,7 @@
BBBD51EA2C36803600557CD3 /* ContactsWorker.swift in Sources */,
BBCC22D92C352ADF00CC19C9 /* MailConfiguration.swift in Sources */,
BBBCE9812CFCBD550061DB04 /* BottomSheetPresentationController.swift in Sources */,
BBEBFB602D3E03E800E566A6 /* WebSocketEndPointProtocol.swift in Sources */,
BBB163A22C3701D800182E37 /* PasswordValidator.swift in Sources */,
BBBCE9722CFCB7D80061DB04 /* LoggerDetailsViewController.swift in Sources */,
BBAFFC2E2C342A0D005703B0 /* UserDefaultsStore.swift in Sources */,
Expand All @@ -607,6 +625,7 @@
BB0A70632CBF822500084289 /* RSAWorker.swift in Sources */,
BBAFFBF62C341CC2005703B0 /* UIView+extension.swift in Sources */,
BBAFFC042C342152005703B0 /* Date+extension.swift in Sources */,
BBEBFB5E2D3E03D600E566A6 /* WebSocketManagerDelegate.swift in Sources */,
BBBD51EC2C369B5700557CD3 /* ImageFilter.swift in Sources */,
BB3C2EFF2C6A0D8900149867 /* ParameterEncoder.swift in Sources */,
BBBCE97A2CFCB8500061DB04 /* NetworkGlobals.swift in Sources */,
Expand All @@ -630,6 +649,7 @@
BBAFFC022C342126005703B0 /* Character+extension.swift in Sources */,
BBAFFC202C34278A005703B0 /* UIImage+extension.swift in Sources */,
BB3B164B2CECAB2C0049F5F2 /* PublishedAction.swift in Sources */,
BBEBFB622D3E03F500E566A6 /* WebSocketManager.swift in Sources */,
BB3B163D2CEC97470049F5F2 /* Provider.swift in Sources */,
BBAFFC002C342012005703B0 /* RegularExpression.swift in Sources */,
BBAFFC282C342862005703B0 /* UICollectionView+extension.swift in Sources */,
Expand Down
55 changes: 55 additions & 0 deletions EasyCode/Source/Network Layer/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class NetworkManager: NSObject {
/// An array of `Data` objects representing the SSL certificates to be used for SSL pinning.
public var certDataItems = [Data]()

private var longPollTimer: Timer?

/// Initializes a new instance of `NetworkManager`.
public override init() {}

Expand Down Expand Up @@ -345,6 +347,59 @@ public class NetworkManager: NSObject {
return composedResponse
}
}

/// Starts a long-polling process to periodically send HTTP requests to the specified endpoint.
///
/// Long-polling involves repeatedly sending requests at a specified interval to fetch updated data.
/// Use the `completion` callback to handle each response, and provide a way to stop the polling.
///
/// - Parameters:
/// - endpoint: An object conforming to `EndPointProtocol` that defines the request details.
/// - interval: The time interval (in seconds) between successive requests.
/// - completion: A closure that gets called with each response. It provides:
/// - `response`: The response object conforming to `NetworkResponseProtocol`.
/// - `stop`: A closure to terminate the long-polling process.
///
/// # Example
/// ```swift
/// networkManager.startLongPolling(endpoint: myEndpoint, interval: 10) { response, stop in
/// if response.success {
/// print("Long-polling succeeded with data: \(response.data ?? Data())")
/// stop()
/// } else {
/// print("Long-polling failed with error: \(response.error?.errorMessage ?? "Unknown error")")
/// }
/// }
/// ```
public func startLongPolling(
endpoint: EndPointProtocol,
interval: TimeInterval,
completion: @escaping (_ response: NetworkResponseProtocol, _ stop: () -> Void) -> Void
) {
stopLongPolling()

longPollTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
guard let self else { return }

self.request(endpoint) { response in
let stopClosure = { [weak self] in
guard let self else { return }
self.stopLongPolling()
}
completion(response, stopClosure)
}
}

longPollTimer?.fire()
}

/// Stops the currently running long-polling process.
///
/// Call this method to manually terminate the long-polling process.
public func stopLongPolling() {
longPollTimer?.invalidate()
longPollTimer = nil
}
}

// MARK: - URLSessionDelegate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// WebSocketEndPointProtocol.swift
// EasyCode
//
// Created by Zhanibek Lukpanov on 20.01.2025.
//

import Foundation

public protocol WebSocketEndPointProtocol {

var url: URL { get }
var headers: [String: String]? { get }
}
Loading
Loading