Xcode 控制台输出
[warn][tid:com.facebook.React.WebSocketExecutor][RCTWebSocketExecutor.m:128] WebSocket connection failed with error Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused"
[warn][tid:com.facebook.react.RCTBridgeQueue][RCTWebSocketExecutor.m:71] Connection to http://localhost:8081/debugger-proxy timed out. Are you running node proxy? If you are running on the device, check if you have the right IP address in `RCTWebSocketExecutor.m`.
[error][tid:main] socket closed
[error][tid:com.facebook.React.WebSocketExecutor] socket closed
解决:
在RCTWebSocketExecutor.m
文件中修改setup
函数中的localhost
为你电脑的IP地址(例如:192.168.0.104)
修改前代码:
- (void)setUp
{
if (!_url) {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSInteger port = [standardDefaults integerForKey:@"websocket-executor-port"] ?: 8081;
NSString *URLString = [NSString stringWithFormat:@"http://localhost:%zd/debugger-proxy", port];
_url = [RCTConvert NSURL:URLString];
}
// 此处省略后面代码
}
修改后代码:
- (void)setUp
{
if (!_url) {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSInteger port = [standardDefaults integerForKey:@"websocket-executor-port"] ?: 8081;
NSString *URLString = [NSString stringWithFormat:@"http://192.168.0.104:%zd/debugger-proxy", port];
_url = [RCTConvert NSURL:URLString];
}
// 此处省略后面代码
}
题外话:在终端检测Node.js所启动服务的端口是否为默认的8081
/Volumes/Transcend/ReactNativeProjects/AwesomeProject/node_modules/react-native/packager ~
┌────────────────────────────────────────────────────────────────────────────┐
│ Running packager on port 8081. │
│ │
│ Keep this packager running while developing on any JS projects. Feel │
│ free to close this tab and run your own packager instance if you │
│ prefer. │
│ │
│ https://github.com/facebook/react-native │
│ │
└────────────────────────────────────────────────────────────────────────────┘
Looking for JS files in