@@ -44,10 +44,10 @@ - (NSString *)getIPAddress:(BOOL)preferIPv4
44
44
NSArray *searchArray = preferIPv4 ?
45
45
@[ IOS_WIFI @" /" IP_ADDR_IPv4, IOS_WIFI @" /" IP_ADDR_IPv6, IOS_CELLULAR @" /" IP_ADDR_IPv4, IOS_CELLULAR @" /" IP_ADDR_IPv6 ] :
46
46
@[ IOS_WIFI @" /" IP_ADDR_IPv6, IOS_WIFI @" /" IP_ADDR_IPv4, IOS_CELLULAR @" /" IP_ADDR_IPv6, IOS_CELLULAR @" /" IP_ADDR_IPv4 ] ;
47
-
47
+
48
48
NSDictionary *addresses = [self getIPAddresses ];
49
49
NSLog (@" addresses: %@ " , addresses);
50
-
50
+
51
51
__block NSString *address;
52
52
[searchArray enumerateObjectsUsingBlock: ^(NSString *key, NSUInteger idx, BOOL *stop)
53
53
{
@@ -60,7 +60,7 @@ - (NSString *)getIPAddress:(BOOL)preferIPv4
60
60
- (NSDictionary *)getIPAddresses
61
61
{
62
62
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity: 8 ];
63
-
63
+
64
64
// retrieve the current interfaces - returns 0 on success
65
65
struct ifaddrs *interfaces;
66
66
if (!getifaddrs (&interfaces)) {
@@ -97,46 +97,44 @@ - (NSDictionary *)getIPAddresses
97
97
return [addresses count ] ? addresses : nil ;
98
98
}
99
99
100
- RCT_EXPORT_METHOD (port: (NSString *)port
100
+ RCT_EXPORT_METHOD (start: (NSString *)port
101
101
root:(NSString *)optroot
102
- localOnly:(NSString *)localhost_only
102
+ localOnly:(BOOL *)localhost_only
103
103
resolver:(RCTPromiseResolveBlock)resolve
104
104
rejecter:(RCTPromiseRejectBlock)reject) {
105
105
106
106
NSString * root;
107
-
107
+
108
108
if ( [optroot length ] == 0 ){
109
109
root = [NSString stringWithFormat: @" %@ " , [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex: 0 ] ];
110
110
} else if ([optroot hasPrefix: @" /" ]) {
111
111
root = optroot;
112
112
} else if ( [optroot isEqualToString: @" BUNDLE" ] ){
113
- root = [NSString stringWithFormat: @" %@ /www" , [[NSBundle mainBundle ] bundlePath ] ];
114
- } else if ( [optroot isEqualToString: @" DOCS" ] ){
115
- root = [NSString stringWithFormat: @" %@ " , [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex: 0 ] ];
113
+ root = [NSString stringWithFormat: @" %@ " , [[NSBundle mainBundle ] bundlePath ] ];
116
114
}
117
-
118
-
115
+
116
+
119
117
if (root) self.www_root = root;
120
-
118
+
121
119
if (port) self.port = [port intValue ];
122
-
123
- if (localhost_only) self.localhost_only = [ localhost_only boolValue ] ;
124
-
120
+
121
+ self.localhost_only = localhost_only;
122
+
125
123
if (self.httpServer != nil ) {
126
124
if ([self .httpServer isRunning ]) {
127
125
NSError *error = nil ;
128
126
reject (@" server_error" , @" server is already up" , error);
129
127
return ;
130
128
}
131
129
}
132
-
130
+
133
131
[DDLog addLogger: [DDTTYLogger sharedInstance ]];
134
132
self.httpServer = [[HTTPServer alloc ] init ];
135
-
133
+
136
134
[self .httpServer setPort: self .port];
137
-
135
+
138
136
if (self.localhost_only ) [self .httpServer setInterface: IP_LOCALHOST];
139
-
137
+
140
138
// Serve files from our embedded Web folder
141
139
const char * docroot = [self .www_root UTF8String ];
142
140
if (*docroot == ' /' ) {
@@ -147,40 +145,39 @@ - (NSDictionary *)getIPAddresses
147
145
}
148
146
NSLog (@" Setting document root: %@ " , self.localPath );
149
147
[self .httpServer setDocumentRoot: self .localPath];
150
-
151
-
148
+
149
+
152
150
NSError *error;
153
151
if ([self .httpServer start: &error]) {
154
152
int listenPort = [self .httpServer listeningPort ];
155
153
NSString * ip = self.localhost_only ? IP_LOCALHOST : [self getIPAddress: YES ];
156
154
NSLog (@" Started httpd on port %d " , listenPort);
157
-
155
+
158
156
self.url = [NSString stringWithFormat: @" http://%@ :%d /" , ip, listenPort];
159
157
resolve (self.url );
160
-
158
+
161
159
} else {
162
160
NSLog (@" Error starting httpd: %@ " , error);
163
-
161
+
164
162
// NSString* errmsg = [error description];
165
163
reject (@" server_error" , @" server could not start" , error);
166
-
164
+
167
165
}
168
-
166
+
169
167
}
170
168
171
169
RCT_EXPORT_METHOD (stop) {
172
170
if (self.httpServer != nil ) {
173
-
171
+
174
172
[self .httpServer stop ];
175
173
self.httpServer = nil ;
176
-
174
+
177
175
self.localPath = @" " ;
178
176
self.url = @" " ;
179
-
177
+
180
178
NSLog (@" httpd stopped" );
181
179
}
182
180
}
183
181
184
182
185
183
@end
186
-
0 commit comments