-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSocketが手動操作以外で切断された場合に自動で再接続を試みるようにした, fix #24
- Loading branch information
1 parent
19b1e1f
commit 75979ed
Showing
9 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Copyright (c) 2022 shibafu | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "DONAutoReconnectStreamingDelegate.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface DONAutoReconnectStreaming : NSObject <DONStreamingEventDelegate> | ||
|
||
@property (nonatomic, weak) id<DONAutoReconnectStreamingDelegate> delegate; | ||
|
||
- (instancetype)init __attribute__((unavailable("init is not available"))); | ||
|
||
- (instancetype)initWithDelegate:(id<DONAutoReconnectStreamingDelegate>)delegate; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Copyright (c) 2022 shibafu | ||
// | ||
|
||
#import "DONAutoReconnectStreaming.h" | ||
|
||
@interface DONAutoReconnectStreaming () | ||
|
||
@property (nonatomic) NSInteger retryCount; | ||
|
||
@end | ||
|
||
@implementation DONAutoReconnectStreaming | ||
|
||
- (instancetype)initWithDelegate:(id<DONAutoReconnectStreamingDelegate>)delegate { | ||
if (self = [super init]) { | ||
_delegate = delegate; | ||
_retryCount = 0; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)donStreamingDidConnect { | ||
if ([self.delegate respondsToSelector:@selector(donStreamingDidConnect)]) { | ||
[self.delegate donStreamingDidConnect]; | ||
} | ||
self.retryCount = 0; // reset retry counter | ||
} | ||
|
||
- (void)donStreamingDidReceiveUpdate:(nonnull DONStatus *)status { | ||
[self.delegate donStreamingDidReceiveUpdate:status]; | ||
} | ||
|
||
- (void)donStreamingDidReceiveNotification:(nonnull DONMastodonNotification *)notification { | ||
[self.delegate donStreamingDidReceiveNotification:notification]; | ||
} | ||
|
||
- (void)donStreamingDidFailWithError:(nonnull NSError *)error { | ||
[self.delegate donStreamingDidFailWithError:error]; | ||
} | ||
|
||
- (void)donStreamingDidCompleteWithCloseCode:(NSURLSessionWebSocketCloseCode)closeCode error:(NSError *)error { | ||
[self.delegate donStreamingDidCompleteWithCloseCode:(NSURLSessionWebSocketCloseCode)closeCode error:error]; | ||
if (closeCode == NSURLSessionWebSocketCloseCodeNormalClosure) { | ||
// cancel retry | ||
self.retryCount = 0; | ||
} else { | ||
// try reconnect | ||
int timeToSleep = MIN(2 << self.retryCount, 60); | ||
self.retryCount += 1; | ||
NSLog(@"streaming finish with error. try reconnect after %d sec.", timeToSleep); | ||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeToSleep * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | ||
if (self.retryCount == 0) { | ||
// cancelled | ||
return; | ||
} | ||
[self.delegate donStreamingShouldReconnect:self]; | ||
}); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright (c) 2022 shibafu | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class DONAutoReconnectStreaming; | ||
|
||
@protocol DONAutoReconnectStreamingDelegate <DONStreamingEventDelegate> | ||
|
||
- (void)donStreamingShouldReconnect:(DONAutoReconnectStreaming *)autoReconnect; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters