Created
October 14, 2021 10:26
-
-
Save louicoder/1a8e239e3249bd6de4cfec37d75ba79f to your computer and use it in GitHub Desktop.
React native websocket connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Text, View, StyleSheet, Alert } from 'react-native'; | |
navigator.__defineGetter__('userAgent', function () { | |
// you have to import rect native first !! | |
return 'react-native'; | |
}); | |
import SocketIOClient from 'socket.io-client/dist/socket.io.js'; | |
// | |
useEffect(() => { | |
const socket = SocketIOClient('wss://site-name.herokuapp.com/', { | |
jsonp: false | |
}); | |
socket.on('connect', () => { | |
console.log('connected'); | |
socket.emit('hello', 'world'); | |
}); | |
socket.on('connect_error', (err) => { | |
console.log(err instanceof Error); | |
console.log(err.message); | |
}); | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment