2015-12-04 17:44:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react-native');
|
|
|
|
var {
|
|
|
|
AppRegistry,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
View
|
|
|
|
} = React;
|
|
|
|
|
|
|
|
global.Buffer = global.Buffer || require('buffer').Buffer;
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
var net = require('net');
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
function randomPort() {
|
|
|
|
return Math.random() * 60536 | 0 + 5000 // 60536-65536
|
|
|
|
}
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
var a = net.createConnection({ port: randomPort() }, function(err) {
|
|
|
|
if (err) throw err
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
console.log('connected');
|
2015-12-04 17:44:16 +00:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
a.on('error', function(err) {
|
|
|
|
console.log(err);
|
2015-12-04 17:44:16 +00:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
var b = net.createConnection({ port: randomPort() }, function(err) {
|
|
|
|
if (err) throw err
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
console.log('connected');
|
2015-12-04 17:44:16 +00:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
b.on('error', function(err) {
|
|
|
|
console.log(err);
|
2015-12-04 17:44:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-12-15 00:16:45 +00:00
|
|
|
// a.on('message', function(data, rinfo) {
|
|
|
|
// var str = String.fromCharCode.apply(null, new Uint8Array(data));
|
|
|
|
// console.log('a received', str, rinfo)
|
|
|
|
// a.close()
|
|
|
|
// b.close()
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// b.on('message', function(data, rinfo) {
|
|
|
|
// var str = String.fromCharCode.apply(null, new Uint8Array(data));
|
|
|
|
// console.log('b received', str, rinfo)
|
|
|
|
//
|
|
|
|
// // echo back
|
|
|
|
// b.send(data, 0, data.length, aPort, '127.0.0.1', function(err) {
|
|
|
|
// if (err) throw err
|
|
|
|
//
|
|
|
|
// console.log('sent')
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// b.once('listening', function() {
|
|
|
|
// var msg = toByteArray('hello')
|
|
|
|
// a.send(msg, 0, msg.length, bPort, '127.0.0.1', function(err) {
|
|
|
|
// if (err) throw err
|
|
|
|
//
|
|
|
|
// console.log('sent')
|
|
|
|
// })
|
|
|
|
// })
|
2015-12-04 17:44:16 +00:00
|
|
|
|
|
|
|
var rctsockets = React.createClass({
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Text style={styles.welcome}>
|
|
|
|
Open Dev Tools to see socket chatter
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: '#F5FCFF',
|
|
|
|
},
|
|
|
|
welcome: {
|
|
|
|
fontSize: 20,
|
|
|
|
textAlign: 'center',
|
|
|
|
margin: 10,
|
|
|
|
},
|
|
|
|
instructions: {
|
|
|
|
textAlign: 'center',
|
|
|
|
color: '#333333',
|
|
|
|
marginBottom: 5,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// // only works for 8-bit chars
|
|
|
|
// function toByteArray(obj) {
|
|
|
|
// var uint = new Uint8Array(obj.length);
|
|
|
|
// for (var i = 0, l = obj.length; i < l; i++){
|
|
|
|
// uint[i] = obj.charCodeAt(i);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return new Uint8Array(uint);
|
|
|
|
// }
|
|
|
|
|
|
|
|
AppRegistry.registerComponent('rctsockets', () => rctsockets);
|