2015-12-23 18:56:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Peel Technologies, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
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() {
|
2015-12-22 23:53:08 +00:00
|
|
|
return Math.random() * 60536 | 0 + 5000; // 60536-65536
|
2015-12-15 00:16:45 +00:00
|
|
|
}
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
var serverPort = randomPort();
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
var server = net.createServer(function(socket) {
|
2015-12-28 19:12:21 +00:00
|
|
|
console.log('server connected on ' + JSON.stringify(socket.address()));
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-22 23:53:08 +00:00
|
|
|
socket.on('data', function (data) {
|
|
|
|
console.log('Server Received: ' + data);
|
|
|
|
socket.write('Echo server\r\n');
|
|
|
|
});
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-22 23:53:08 +00:00
|
|
|
socket.on('error', function(error) {
|
|
|
|
console.log('error ' + error);
|
|
|
|
});
|
2015-12-28 19:12:21 +00:00
|
|
|
}).listen(serverPort, function() {
|
2015-12-23 23:14:41 +00:00
|
|
|
console.log('opened server on ' + JSON.stringify(server.address()));
|
|
|
|
});
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
server.on('error', function(error) {
|
|
|
|
console.log('error ' + error);
|
|
|
|
});
|
|
|
|
|
|
|
|
var client = net.createConnection(serverPort, function() {
|
2015-12-23 23:14:41 +00:00
|
|
|
console.log('opened client on ' + JSON.stringify(client.address()));
|
|
|
|
client.write('Hello, server! Love, Client.');
|
2015-12-22 23:53:08 +00:00
|
|
|
});
|
|
|
|
|
2015-12-23 23:14:41 +00:00
|
|
|
client.on('data', function(data) {
|
2015-12-28 19:12:21 +00:00
|
|
|
console.log('Client Received: ' + data);
|
2015-12-23 23:14:41 +00:00
|
|
|
client.destroy(); // kill client after server's response
|
|
|
|
server.close();
|
2015-12-22 23:53:08 +00:00
|
|
|
});
|
2015-12-04 17:44:16 +00:00
|
|
|
|
2015-12-28 19:12:21 +00:00
|
|
|
client.on('error', function(error) {
|
|
|
|
console.log('client error ' + error);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('close', function() {
|
|
|
|
console.log('client close');
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
AppRegistry.registerComponent('rctsockets', () => rctsockets);
|