forked from Blendman974/faceclaw
iOS porting: Battery indicators, app icon, add the rest of the apps
This commit is contained in:
1 parent
00a5e38f75
commit
e5ba7abc18
31 files changed
+501
-65
No files matched your search
@@ -0,0 +1,8 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** Thread-safe event queue; each NativeScript worker drains it on its own thread. */
|
||||
@interface FaceclawSocket : NSObject <NSURLSessionWebSocketDelegate>
|
||||
- (instancetype)initWithURL:(NSString *)url;
|
||||
- (NSString *)takeEvents;
|
||||
- (void)sendText:(NSString *)text;
|
||||
- (void)close;
|
||||
@end
|
||||
@@ -0,0 +1,75 @@
|
||||
#import "FaceclawSocket.h"
|
||||
@interface FaceclawSocket ()
|
||||
@property(nonatomic, strong) NSURLSession *session;
|
||||
@property(nonatomic, strong) NSURLSessionWebSocketTask *task;
|
||||
@property(nonatomic, strong) NSMutableArray *events;
|
||||
@property(nonatomic) BOOL ended;
|
||||
@property(nonatomic) NSUInteger queuedBytes;
|
||||
@end
|
||||
@implementation FaceclawSocket
|
||||
- (instancetype)initWithURL:(NSString *)url {
|
||||
if ((self = [super init])) {
|
||||
_events = [NSMutableArray array];
|
||||
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
|
||||
config.timeoutIntervalForRequest = 20;
|
||||
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
|
||||
_task = [_session webSocketTaskWithURL:[NSURL URLWithString:url]];
|
||||
_task.maximumMessageSize = 8 * 1024 * 1024;
|
||||
[_task resume];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (void)enqueue:(NSDictionary *)event {
|
||||
@synchronized(self) {
|
||||
if (_ended) return;
|
||||
_queuedBytes += [event[@"text"] lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
||||
if (_events.count >= 256 || _queuedBytes > 16 * 1024 * 1024) {
|
||||
[_events removeAllObjects]; _queuedBytes = 0;
|
||||
[_events addObject:@{@"kind": @"error", @"text": @"Terminal input queue overflow"}];
|
||||
[self close];
|
||||
} else [_events addObject:event];
|
||||
}
|
||||
}
|
||||
- (NSString *)takeEvents {
|
||||
@synchronized(self) {
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:_events options:0 error:nil];
|
||||
[_events removeAllObjects]; _queuedBytes = 0;
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]";
|
||||
}
|
||||
}
|
||||
- (void)receiveNext {
|
||||
__weak FaceclawSocket *weakSelf = self;
|
||||
[_task receiveMessageWithCompletionHandler:^(NSURLSessionWebSocketMessage *message, NSError *error) {
|
||||
FaceclawSocket *owner = weakSelf; if (!owner) return;
|
||||
@synchronized(owner) { if (owner.ended) return; }
|
||||
if (error) { [owner enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [owner close]; return; }
|
||||
if (message.type == NSURLSessionWebSocketMessageTypeString)
|
||||
[owner enqueue:@{@"kind": @"text", @"text": message.string}];
|
||||
[owner receiveNext];
|
||||
}];
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didOpenWithProtocol:(NSString *)protocol {
|
||||
[self enqueue:@{@"kind": @"open"}]; [self receiveNext];
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session webSocketTask:(NSURLSessionWebSocketTask *)task didCloseWithCode:(NSURLSessionWebSocketCloseCode)code reason:(NSData *)reason {
|
||||
[self enqueue:@{@"kind": @"closed", @"code": @(code), @"text": reason ? ([[NSString alloc] initWithData:reason encoding:NSUTF8StringEncoding] ?: @"") : @""}];
|
||||
[self close];
|
||||
}
|
||||
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
|
||||
if (error) [self enqueue:@{@"kind": @"error", @"text": error.localizedDescription}];
|
||||
[self close];
|
||||
}
|
||||
- (void)sendText:(NSString *)text {
|
||||
__weak FaceclawSocket *weakSelf = self;
|
||||
[_task sendMessage:[[NSURLSessionWebSocketMessage alloc] initWithString:text] completionHandler:^(NSError *error) {
|
||||
if (error) { [weakSelf enqueue:@{@"kind": @"error", @"text": error.localizedDescription}]; [weakSelf close]; }
|
||||
}];
|
||||
}
|
||||
- (void)close {
|
||||
@synchronized(self) {
|
||||
if (_ended) return; _ended = YES;
|
||||
[_task cancelWithCloseCode:NSURLSessionWebSocketCloseCodeNormalClosure reason:nil];
|
||||
[_session invalidateAndCancel]; _session = nil;
|
||||
}
|
||||
}
|
||||
@end
|
||||
@@ -1,5 +1,6 @@
|
||||
module FaceclawGraphics {
|
||||
header "FaceclawGraphics.h"
|
||||
header "FaceclawBluetooth.h"
|
||||
header "FaceclawSocket.h"
|
||||
export *
|
||||
}
|
||||
Reference in new issue
Block a user