-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
77 lines (71 loc) · 2.15 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
import React, { useState } from "react";
import { Text, View, Button, TextInput } from "react-native";
import { styled } from "nativewind";
const StyledView = styled(View);
const StyledText = styled(Text);
const Box = ({ className = "", ...props }) => (
<StyledText
className={`flex p-5 w-4/5 m-2 justify-center items-center text-center text-black bg-fuchsia-500 rounded ${className}`}
{...props}
/>
);
export default () => {
const [text, setText] = useState("");
const [name, setName] = useState("");
const [serverMessages, setServerMessages] = React.useState([]);
const ws = new WebSocket("wss://620e-71-247-250-209.ngrok-free.app");
React.useEffect(() => {
const serverMessagesList = [];
ws.onopen = () => {
console.log("Connected to the server");
};
ws.onclose = (e) => {
console.log("Disconnected. Check internet or server.");
};
ws.onerror = (e) => {
console.log(e.message);
};
ws.onmessage = (e) => {
serverMessagesList.push(e.data);
setServerMessages([...serverMessagesList]);
};
}, []);
const submit = () => {
ws.send(JSON.stringify({ name, text }));
setText("");
};
return (
<StyledView className="flex flex-1 pt-12 flex-col justify-start items-center">
<View>
<TextInput
style={{ height: 40 }}
placeholder="Type your name here!"
onChangeText={(v) => setName(v)}
value={name}
/>
</View>
<View className="w-4/5 text-center items-center flex flex-col justify-end h-72 bg-fuchsia-200 pb-5">
{serverMessages.map((e, i) => (
<Text key={"txt" + i}>{e}</Text>
))}
</View>
<Box className="h-14 flex-none bg-fuchsia-200">
<Text>What's the worlds larges mammal?</Text>
</Box>
<View>
<TextInput
style={{ height: 40 }}
placeholder="Type your answer here!"
onChangeText={(v) => setText(v)}
value={text}
/>
</View>
<Button
onPress={submit}
title="Submit"
color="#841584"
accessibilityLabel="Learn more about this purple button"
/>
</StyledView>
);
};