Skip to content

Instantly share code, notes, and snippets.

@brysgo
Created July 26, 2015 03:52
Show Gist options
  • Save brysgo/17b0536381632eb67c27 to your computer and use it in GitHub Desktop.
Save brysgo/17b0536381632eb67c27 to your computer and use it in GitHub Desktop.
#import "AppDelegate.h"
#import "RCTRootView.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *env = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"environment" ofType:@"plist"]];
NSString *serverURLString = [NSString stringWithFormat:@"http://%@/index.ios.bundle", [env objectForKey:@"hostname"]];
NSURL *codeURL = [NSURL URLWithString:serverURLString];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:codeURL
moduleName:@"ChatApp"
launchOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
// server/proxy.js
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
module.exports = function(http) {
return http.createServer(function(req, res) {
if (req.url.indexOf('index.ios') != -1) {
proxy.web(req, res, { target: 'http://localhost:8081' });
}
});
}
// server/server.js
require('react-native/packager/packager.js');
var http = require('./proxy')(require('http'));
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
// js/utils/WebAPIUtils.js
window.navigator.userAgent = "react-native";
var ServerActionCreators = require('../actions/ServerActionCreators');
var io = require('../../node_modules/socket.io/node_modules/socket.io-client/socket.io');
var React = require('react-native');
var EnvironmentManager = require('react-native-env');
var socket;
var serverUrl;
EnvironmentManager.get('hostname')
.then((val) => {
serverUrl = 'http://' + val;
socket = io(serverUrl, {jsonp: false});
socket.on('connect', function(){
socket.on('chat message', function(msg) {
ServerActionCreators.receiveMessage(msg);
});
});
})
module.exports = {
sendMessage: function(msg) {
socket.emit('chat message', msg);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment