-
Notifications
You must be signed in to change notification settings - Fork 213
/
index.ios.js
132 lines (109 loc) · 2.85 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import './shim';
import React, { Component } from 'react';
import {
AppRegistry,
ScrollView,
StyleSheet,
Text,
View
} from 'react-native';
var net = require('net');
function randomPort() {
return Math.random() * 60536 | 0 + 5000; // 60536-65536
}
var serverPort = randomPort();
class RctSockets extends Component {
constructor(props) {
super(props);
this.updateChatter = this.updateChatter.bind(this);
this.state = { chatter: [] };
}
updateChatter(msg) {
this.setState({
chatter: this.state.chatter.concat([msg])
});
}
componentDidMount() {
let server = net.createServer((socket) => {
this.updateChatter('server connected on ' + JSON.stringify(socket.address()));
socket.on('data', (data) => {
this.updateChatter('Server Received: ' + data);
socket.write('Echo server\r\n');
});
socket.on('error', (error) => {
this.updateChatter('error ' + error);
});
socket.on('close', (error) => {
this.updateChatter('server client closed ' + (error ? error : ''));
});
}).listen(serverPort, () => {
this.updateChatter('opened server on ' + JSON.stringify(server.address()));
});
server.on('error', (error) => {
this.updateChatter('error ' + error);
});
server.on('close', () => {
this.updateChatter('server close');
});
let client = net.createConnection(serverPort, () => {
this.updateChatter('opened client on ' + JSON.stringify(client.address()));
client.write('Hello, server! Love, Client.');
});
client.on('data', (data) => {
this.updateChatter('Client Received: ' + data);
this.client.destroy(); // kill client after server's response
this.server.close();
});
client.on('error', (error) => {
this.updateChatter('client error ' + error);
});
client.on('close', () => {
this.updateChatter('client close');
});
this.server = server;
this.client = client;
}
componentWillUnmount() {
this.server = null;
this.client = null;
}
render() {
return (
<View style={styles.container}>
<ScrollView>
{this.state.chatter.map((msg, index) => {
return (
<Text key={index} style={styles.welcome}>
{msg}
</Text>
);
})}
</ScrollView>
</View>
);
}
}
const 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);