Skip to content

Commit 2c9599b

Browse files
committed
feat(ws): setup the WebSocket client
1 parent 987db5e commit 2c9599b

File tree

2 files changed

+79
-69
lines changed

2 files changed

+79
-69
lines changed

App.js

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,14 @@ import {
1616
StatusBar,
1717
} from 'react-native';
1818

19-
import {
20-
Header,
21-
LearnMoreLinks,
22-
Colors,
23-
DebugInstructions,
24-
ReloadInstructions,
25-
} from 'react-native/Libraries/NewAppScreen';
19+
import {Header, Colors} from 'react-native/Libraries/NewAppScreen';
2620

27-
const App: () => React$Node = () => {
28-
return (
29-
<>
30-
<StatusBar barStyle="dark-content" />
31-
<SafeAreaView>
32-
<ScrollView
33-
contentInsetAdjustmentBehavior="automatic"
34-
style={styles.scrollView}>
35-
<Header />
36-
{global.HermesInternal == null ? null : (
37-
<View style={styles.engine}>
38-
<Text style={styles.footer}>Engine: Hermes</Text>
39-
</View>
40-
)}
41-
<View style={styles.body}>
42-
<View style={styles.sectionContainer}>
43-
<Text style={styles.sectionTitle}>Step One</Text>
44-
<Text style={styles.sectionDescription}>
45-
Edit <Text style={styles.highlight}>App.js</Text> to change this
46-
screen and then come back to see your edits.
47-
</Text>
48-
</View>
49-
<View style={styles.sectionContainer}>
50-
<Text style={styles.sectionTitle}>See Your Changes</Text>
51-
<Text style={styles.sectionDescription}>
52-
<ReloadInstructions />
53-
</Text>
54-
</View>
55-
<View style={styles.sectionContainer}>
56-
<Text style={styles.sectionTitle}>Debug</Text>
57-
<Text style={styles.sectionDescription}>
58-
<DebugInstructions />
59-
</Text>
60-
</View>
61-
<View style={styles.sectionContainer}>
62-
<Text style={styles.sectionTitle}>Learn More</Text>
63-
<Text style={styles.sectionDescription}>
64-
Read the docs to discover what to do next:
65-
</Text>
66-
</View>
67-
<LearnMoreLinks />
68-
</View>
69-
</ScrollView>
70-
</SafeAreaView>
71-
</>
72-
);
73-
};
21+
import {format} from 'date-fns';
7422

7523
const styles = StyleSheet.create({
7624
scrollView: {
7725
backgroundColor: Colors.lighter,
7826
},
79-
engine: {
80-
position: 'absolute',
81-
right: 0,
82-
},
8327
body: {
8428
backgroundColor: Colors.white,
8529
},
@@ -98,17 +42,56 @@ const styles = StyleSheet.create({
9842
fontWeight: '400',
9943
color: Colors.dark,
10044
},
101-
highlight: {
102-
fontWeight: '700',
103-
},
104-
footer: {
105-
color: Colors.dark,
106-
fontSize: 12,
107-
fontWeight: '600',
108-
padding: 4,
109-
paddingRight: 12,
110-
textAlign: 'right',
111-
},
11245
});
11346

47+
const App: () => React$Node = () => {
48+
const [time, setTime] = React.useState(undefined);
49+
const handleMessage = React.useCallback(({data}) => {
50+
try {
51+
const date = new Date(data);
52+
setTime(date);
53+
} catch (err) {
54+
console.warn('error data', {data});
55+
}
56+
}, []);
57+
58+
React.useEffect(() => {
59+
const ws = new WebSocket('ws://localhost:8080');
60+
ws.onopen = async () => {
61+
console.warn('open');
62+
};
63+
ws.onmessage = handleMessage;
64+
ws.onerror = err => {
65+
console.warn('onerror', {err});
66+
};
67+
ws.onclose = () => {
68+
console.warn('closed');
69+
};
70+
71+
return ws.close.bind(ws);
72+
}, [handleMessage]);
73+
return (
74+
<>
75+
<StatusBar barStyle="dark-content" />
76+
<SafeAreaView>
77+
<ScrollView
78+
contentInsetAdjustmentBehavior="automatic"
79+
style={styles.scrollView}>
80+
<Header />
81+
<View style={styles.body}>
82+
{!!time && (
83+
<View style={styles.sectionContainer}>
84+
<Text style={styles.sectionTitle}>Timer</Text>
85+
<Text style={styles.sectionDescription}>
86+
{format(time, 'yyyy/MM/dd H:mm:ss.SSS') + ' '}
87+
</Text>
88+
</View>
89+
)}
90+
</View>
91+
</ScrollView>
92+
</SafeAreaView>
93+
</>
94+
);
95+
};
96+
11497
export default App;

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@ After you have installed it, you need to start the `WebSocket` and you can do th
1313
# it means the server is running on "ws://localhost:8080"
1414
deno --allow-net ./web-socket-server/index.ts
1515
```
16+
17+
## Running WebSocket client with [React Native](https://facebook.github.io/react-native/)
18+
19+
### Prerequirements
20+
21+
In order to run this, you must have the React Native installed and configured and `yarn` (or `npm` installed).
22+
23+
### Installing
24+
25+
Before installing the app, you need to have a device or emulator connected to your machine and run:
26+
27+
```bash
28+
# To install all the dependencies
29+
yarn # Or "npm install"
30+
# To install the app on the device or emulator
31+
yarn android # or "npm run android" or even "yarn ios" ("npm ios") for iOS
32+
# Now wee need to redirect all calls from device "localhost:8080" to our machine "localhost:8080"
33+
adb reverse tcp:8080 tcp:8080
34+
```
35+
36+
If the `WebSocket` client didn't connect, try to reload your app by shaking your phone and pressing "Reload" or:
37+
38+
```bash
39+
# Only valid for Android devices/emulators
40+
adb shell input text "RR"
41+
```
42+

0 commit comments

Comments
 (0)