-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
154 lines (124 loc) · 5.09 KB
/
App.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Text,
View,
Button,
TextInput
} from 'react-native';
import { NetworkInfo } from 'react-native-network-info';
import TCP from 'react-native-tcp';
import KeepAwake from 'react-native-keep-awake';
import UDP from 'react-native-udp';
const PORT = 55666;
export default class App extends Component {
constructor(props) {
super(props);
this.state = {server: null, socket: null, client: null, myIpAddress: '', serverIpAddress: '', clients: []}
}
componentDidMount = () => {
NetworkInfo.getIPV4Address(ip => {
this.setState({myIpAddress: ip});
});
}
render() {
return (
<View>
<Text>My IP Address: {this.state.myIpAddress}</Text>
{(this.state.server === null && this.state.client === null) && <Button title="Start Host" onPress={this.startHost} />}
{(this.state.server !== null && this.state.client === null) && <Button title="Stop Host" onPress={this.stopHost} />}
{this.state.server !== null && <View style={{paddingTop: 10}}><Button title="Send data packet" onPress={this.sendServerPacket}/></View>}
{this.state.server === null && <View>
<View style={{height: 50}} />
<Text>Enter remote IP address to connect to below</Text>
<TextInput returnKeyType="done" keyboardType='numeric' value={this.state.serverIpAddress} onChangeText={this.onIPEntered} onSubmitEditing={this.enterPressed}/>
{this.state.client === null && <Button title="Start Client" onPress={this.startClient} disabled={this.state.serverIpAddress === ''}/>}
{this.state.client !== null && <Button title="Stop Client" onPress={this.stopClient}/>}
{this.state.client !== null && <View style={{paddingTop: 10}}><Button title="Send Data Packet" onPress={this.sendClientPacket}/></View>}
</View>}
</View>
);
}
startClient = () => {
// let client = TCP.createConnection({host: this.state.serverIpAddress, port: PORT}, () => {
// client.write("Initial Client Packet");
// });
// client.on('data', (data) => {
// console.warn('Client Received data: '+data.toString('ascii'));
// });
// client.on('error', (data) => {
// console.warn('Error on client', data.toString('ascii'));
// });
// this.setState({client});
let client = UDP.createSocket('udp4');
client.bind(PORT);
client.once('listening', () => {
let msg = new Buffer("INIT");
client.send(msg, 0, msg.length, PORT, this.state.serverIpAddress);
});
client.on('message', (message, rinfo) => {
console.warn('Client Received data: '+message);
});
this.setState({client});
}
stopClient = () => {
this.state.client.close();
this.setState({client: null});
}
sendClientPacket = () => {
// this.state.client.write("Packet from client");
// let msg = toByteArray("Packet from client")
let msg = new Buffer("Packet from client");
this.state.client.send(msg, 0, msg.length, PORT, this.state.serverIpAddress);
}
onIPEntered = (val) => {
this.setState({serverIpAddress: val});
}
enterPressed = (event) => {
this.onIPEntered(event.nativeEvent.text);
}
startHost = () => {
KeepAwake.activate();
// let server = TCP.createServer((socket) => {
// socket.on('data', (data) => {
// console.warn('Server Received data: '+data.toString('ascii'));
// });
// socket.on('error', (e) => {
// console.warn('Error on server socket: '+e.toString('ascii'));
// });
// this.setState({socket});
// }).listen({port: PORT}, () => {});
let server = UDP.createSocket('udp4');
server.bind(PORT);
server.on('message', (message, rinfo) => {
console.warn('Server Received data: '+message);
if(message == "INIT") {
let ip = rinfo.address;
console.warn('Init received from '+ip);
if(this.state.clients.indexOf(ip) === -1) {
let clients = [...this.state.clients];
clients.push(ip);
this.setState({clients});
}
return;
}
});
this.setState({server});
}
stopHost = () => {
KeepAwake.deactivate();
this.state.server.close();
this.setState({server: null, socket: null});
}
sendServerPacket = () => {
for(let i = 0; i < this.state.clients.length; i++) {
let msg = new Buffer("Packet from server");
this.state.server.send(msg, 0, msg.length, PORT, this.state.clients[i]);
}
// this.state.socket.write("Packet from server");
}
}