forked from tradle/react-native-udp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
127 lines (109 loc) · 2.84 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
ScrollView,
StyleSheet,
Text,
View
} from 'react-native';
import base64 from 'base64-js';
import dgram from 'dgram';
function randomPort() {
return Math.random() * 60536 | 0 + 5000 // 60536-65536
}
// 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);
}
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 self = this;
let a = dgram.createSocket('udp4');
let aPort = randomPort();
a.bind(aPort, function(err) {
if (err) throw err;
self.updateChatter('a bound to ' + JSON.stringify(a.address()));
})
let b = dgram.createSocket('udp4');
var bPort = randomPort();
b.bind(bPort, function(err) {
if (err) throw err;
self.updateChatter('b bound to ' + JSON.stringify(b.address()));
})
a.on('message', function(data, rinfo) {
var str = String.fromCharCode.apply(null, new Uint8Array(data));
self.updateChatter('a received echo ' + str + ' ' + JSON.stringify(rinfo));
a.close();
b.close();
})
b.on('message', function(data, rinfo) {
var str = String.fromCharCode.apply(null, new Uint8Array(data));
self.updateChatter('b received ' + str + ' ' + JSON.stringify(rinfo));
// echo back
b.send(data, 0, data.length, aPort, '127.0.0.1', function(err) {
if (err) throw err
self.updateChatter('b echoed data')
})
})
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
self.updateChatter('a sent data')
})
})
}
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);