forked from socketio/socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples/react-native): add example with React Native
Includes socketio/engine.io-client@e5bc106
- Loading branch information
1 parent
5d16319
commit 7a219f9
Showing
13 changed files
with
6,953 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"12bb71342c6255bbf50437ec8f4441c083f47cdb74bd89160c15e4f43e52a1cb": true, | ||
"40b842e832070c58deac6aa9e08fa459302ee3f9da492c7e77d93d2fbf4a56fd": true | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
node_modules/**/* | ||
.expo/* | ||
npm-debug.* | ||
*.jks | ||
*.p8 | ||
*.p12 | ||
*.key | ||
*.mobileprovision | ||
*.orig.* | ||
web-build/ | ||
web-report/ | ||
|
||
# macOS | ||
.DS_Store |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { Component } from 'react'; | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
import io from 'socket.io-client'; | ||
|
||
const socket = io("192.168.0.28:3000"); // replace with the IP of your server, when testing on real devices | ||
|
||
export default class App extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
connected: socket.connected, | ||
currentTransport: socket.connected ? socket.io.engine.transport.name : '-', | ||
lastMessage: "" | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
socket.on('connect', () => this.onConnectionStateUpdate()); | ||
socket.on('disconnect', () => this.onConnectionStateUpdate()); | ||
socket.on('message', (content) => this.onMessage(content)); | ||
} | ||
|
||
componentWillUnmount() { | ||
socket.off('connect'); | ||
socket.off('disconnect'); | ||
socket.off('message'); | ||
} | ||
|
||
onConnectionStateUpdate() { | ||
this.setState({ | ||
connected: socket.connected, | ||
currentTransport: socket.connected ? socket.io.engine.transport.name : '-' | ||
}); | ||
if (socket.connected) { | ||
socket.io.engine.on('upgrade', () => this.onUpgrade()); | ||
} else { | ||
socket.io.engine.off('upgrade'); | ||
} | ||
} | ||
|
||
onMessage(content) { | ||
this.setState({ | ||
lastMessage: content | ||
}); | ||
} | ||
|
||
onUpgrade() { | ||
this.setState({ | ||
currentTransport: socket.io.engine.transport.name | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<View style={styles.container}> | ||
<Text>State: { this.state.connected ? 'Connected' : 'Disconnected' }</Text> | ||
<Text>Current transport: { this.state.currentTransport }</Text> | ||
<Text>Last message: { this.state.lastMessage }</Text> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: '#fff', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
# Example with [React Native](https://reactnative.dev/) | ||
|
||
 | ||
|
||
This example shows a basic example based on [Expo](https://expo.io/). | ||
|
||
 | ||
|
||
## How to use | ||
|
||
``` | ||
$ npm ci | ||
$ npm start # run expo | ||
$ node server.js # run the server | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"expo": { | ||
"name": "react-native", | ||
"slug": "react-native", | ||
"platforms": [ | ||
"ios", | ||
"android", | ||
"web" | ||
], | ||
"version": "1.0.0", | ||
"orientation": "portrait", | ||
"icon": "./assets/icon.png", | ||
"splash": { | ||
"image": "./assets/splash.png", | ||
"resizeMode": "contain", | ||
"backgroundColor": "#ffffff" | ||
}, | ||
"updates": { | ||
"fallbackToCacheTimeout": 0 | ||
}, | ||
"assetBundlePatterns": [ | ||
"**/*" | ||
], | ||
"ios": { | ||
"supportsTablet": true | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = function(api) { | ||
api.cache(true); | ||
return { | ||
presets: ['babel-preset-expo'], | ||
}; | ||
}; |
Oops, something went wrong.