mirror of
https://github.com/status-im/react-native-tcp.git
synced 2025-01-27 10:44:45 +00:00
106 lines
2.1 KiB
JavaScript
106 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
var React = require('react-native');
|
|
var {
|
|
AppRegistry,
|
|
StyleSheet,
|
|
Text,
|
|
View
|
|
} = React;
|
|
|
|
global.Buffer = global.Buffer || require('buffer').Buffer;
|
|
|
|
var net = require('net');
|
|
|
|
function randomPort() {
|
|
return Math.random() * 60536 | 0 + 5000; // 60536-65536
|
|
}
|
|
|
|
var aPort = randomPort();
|
|
|
|
var a = net.createServer({}, function(socket) {
|
|
console.log('server connected');
|
|
|
|
// socket.on('data', function (data) {
|
|
// var str = String.fromCharCode.apply(null, new Uint8Array(data));
|
|
// console.log('a received', str);
|
|
// a.close();
|
|
// b.end();
|
|
// });
|
|
|
|
socket.on('data', function (data) {
|
|
console.log('Server Received: ' + data);
|
|
socket.write('Echo server\r\n');
|
|
});
|
|
|
|
socket.on('error', function(error) {
|
|
console.log('error ' + error);
|
|
});
|
|
}).listen({ port: aPort });
|
|
|
|
// a.on('listening', function() {
|
|
// console.log('listening');
|
|
// });
|
|
//
|
|
// a.on('error', function(error) {
|
|
// console.log('error ' + error);
|
|
// });
|
|
|
|
var b = net.createConnection({ port: aPort }, function(err) {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
|
|
console.log('client connected');
|
|
b.write('Hello, server! Love, Client.');
|
|
});
|
|
|
|
b.on('data', function(data) {
|
|
console.log('Client Received: ' + data);
|
|
b.end(); // kill client after server's response
|
|
a.close();
|
|
});
|
|
|
|
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);
|