-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
142 lines (124 loc) · 3.14 KB
/
Copy pathApp.js
File metadata and controls
142 lines (124 loc) · 3.14 KB
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
/* global WebSocket */
import React, { Component } from 'react'
import {
StyleSheet,
Text,
View,
Button,
ToastAndroid
} from 'react-native'
// FIXME
const ENDPOINT = '0.0.0.0'
export default class App extends Component {
constructor (props) {
super(props)
this.state = { connected: false }
this.onPressUp = this.onPressUp.bind(this)
this.onPressDown = this.onPressDown.bind(this)
this.onPressRight = this.onPressRight.bind(this)
this.onPressLeft = this.onPressLeft.bind(this)
this.onPressConnect = this.onPressConnect.bind(this)
this.onPressDisconnect = this.onPressDisconnect.bind(this)
}
onPressConnect () {
let ws = new WebSocket(`ws://${ENDPOINT}`)
ws.onopen = () => {
ToastAndroid.show(
'Connected',
ToastAndroid.SHORT
)
this.setState({ connected: true })
}
ws.onerror = (e) => {
ToastAndroid.show('Error: ' + e.message, ToastAndroid.SHORT)
}
this.setState({ ws: ws })
}
onPressDisconnect () {
this.state.ws.close()
this.state.ws.onclose = () => {
ToastAndroid.show(
'Disconnected',
ToastAndroid.SHORT
)
}
this.setState({
connected: false
})
}
onPressUp () {
this.state.ws.send('1')
setTimeout(() => {
this.state.ws.send('-1')
}, 100)
}
onPressDown () {
this.state.ws.send('3')
setTimeout(() => {
this.state.ws.send('-3')
}, 100)
}
onPressRight () {
this.state.ws.send('4')
setTimeout(() => {
this.state.ws.send('-4')
}, 100)
}
onPressLeft () {
this.state.ws.send('2')
setTimeout(() => {
this.state.ws.send('-2')
}, 100)
}
render () {
return (
<View style={styles.container}>
<View style={styles.wrapper}>
<Button
title={this.state.connected ? 'Disconnect' : 'Connect'}
color={this.state.connected ? '#dd2c00' : '#64dd17'}
onPress={this.state.connected ? this.onPressDisconnect : this.onPressConnect}
style={styles.button} />
<Text style={{ paddingTop: 10, color: '#808080' }}>{this.state.connected ? 'Connected' : 'Disconnected'}</Text>
</View>
<View style={styles.wrapper}>
<Button
title='Up'
onPress={this.onPressUp}
style={styles.button} />
</View>
<View style={{flexDirection: 'row'}}>
<View style={styles.wrapper}>
<Button
title='Right'
onPress={this.onPressRight}
style={styles.button} />
</View>
<View style={styles.wrapper}>
<Button
title='Left'
onPress={this.onPressLeft}
style={styles.button} />
</View>
</View>
<View style={styles.wrapper}>
<Button
title='Down'
onPress={this.onPressDown}
style={styles.button} />
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
},
wrapper: {
padding: 10
}
})