Answer the question
In order to leave comments, you need to log in
How to work with web sockets in react-native?
Good Sunday evening everyone. I FE dev
decided to look at React native.
Launched a server on EC2 (AWS).
Wrote this code
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', (req, res) => {
res.send('Hello world!');
})
io.on('connection', (socket) => {
socket.on('start', () => {
socket.emit('begin', true);
});
});
server.listen(3000, () => console.log('Server started!'));
....
buttonHandle = () => {
const socket = io("ws://blablabla/");
socket.emit('start');
socket.on('begin', function (data) {
console.warn(data);
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.boom}>Boom!</Text>
<Button title='Activate' onPress={this.buttonHandle} />
</View>
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question