RCTReconnectingWebSocket.m
3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTReconnectingWebSocket.h"
#import <React/RCTConvert.h>
#import <React/RCTDefines.h>
#import <React/fishhook.h>
#import <React/RCTLog.h>
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
#import <os/log.h>
#endif /* __IPHONE_11_0 */
#import "RCTSRWebSocket.h"
#if RCT_DEV // Only supported in dev mode
static void (*orig_nwlog_legacy_v)(int, char*, va_list);
static void my_nwlog_legacy_v(int level, char *format, va_list args) {
static const uint buffer_size = 256;
static char buffer[buffer_size];
va_list copy;
va_copy(copy, args);
vsnprintf(buffer, buffer_size, format, copy);
va_end(copy);
if (strstr(buffer, "nw_connection_get_connected_socket_block_invoke") == NULL &&
strstr(buffer, "Connection has no connected handler") == NULL) {
orig_nwlog_legacy_v(level, format, args);
}
}
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
static void (*orig_os_log_error_impl)(void *dso, os_log_t log, os_log_type_t type, const char *format, uint8_t *buf, uint32_t size);
static void my_os_log_error_impl(void *dso, os_log_t log, os_log_type_t type, const char *format, uint8_t *buf, uint32_t size)
{
if (strstr(format, "TCP Conn %p Failed : error %ld:%d") == NULL) {
orig_os_log_error_impl(dso, log, type, format, buf, size);
}
}
#endif /* __IPHONE_11_0 */
@interface RCTReconnectingWebSocket () <RCTSRWebSocketDelegate>
@end
@implementation RCTReconnectingWebSocket {
NSURL *_url;
RCTSRWebSocket *_socket;
}
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
rebind_symbols((struct rebinding[1]){
{"nwlog_legacy_v", my_nwlog_legacy_v, (void *)&orig_nwlog_legacy_v}
}, 1);
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
rebind_symbols((struct rebinding[1]){
{"_os_log_error_impl", my_os_log_error_impl, (void *)&orig_os_log_error_impl}
}, 1);
#endif /* __IPHONE_11_0 */
});
}
- (instancetype)initWithURL:(NSURL *)url queue:(dispatch_queue_t)queue
{
if (self = [super init]) {
_url = url;
_delegateDispatchQueue = queue;
}
return self;
}
- (instancetype)initWithURL:(NSURL *)url
{
return [self initWithURL:url queue:dispatch_get_main_queue()];
}
- (void)send:(id)data
{
[_socket send:data];
}
- (void)start
{
if(!RCTLOG_ENABLED){
return;
}
[self stop];
_socket = [[RCTSRWebSocket alloc] initWithURL:_url];
_socket.delegate = self;
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
[_socket open];
}
- (void)stop
{
_socket.delegate = nil;
[_socket closeWithCode:1000 reason:@"Invalidated"];
_socket = nil;
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didReceiveMessage:(id)message
{
[_delegate reconnectingWebSocket:self didReceiveMessage:message];
}
- (void)reconnect
{
__weak RCTSRWebSocket *socket = _socket;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Only reconnect if the observer wasn't stoppped while we were waiting
if (socket) {
[self start];
}
});
}
- (void)webSocketDidOpen:(RCTSRWebSocket *)webSocket
{
[_delegate reconnectingWebSocketDidOpen:self];
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didFailWithError:(NSError *)error
{
[_delegate reconnectingWebSocketDidClose:self];
[self reconnect];
}
- (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
{
[_delegate reconnectingWebSocketDidClose:self];
[self reconnect];
}
@end
#endif