Enables WebSocket ping and pong messages to be sent from js code.
$ npm install react-native-websocket-impl --save
$ react-native link react-native-websocket-impl
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-websocket-impl
and addRNWebSocketImpl.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNWebSocketImpl.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import com.iskander508.WebSocketImpl.RNWebSocketImplPackage;
to the imports at the top of the file - Add
new RNWebSocketImplPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-websocket-impl' project(':react-native-websocket-impl').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-websocket-impl/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-websocket-impl')
import openConnection from 'react-native-websocket-impl';
openConnection(url, headers)
.then(socket => {
console.log('Socket created successfully!')
socket.on('open', () => console.log('OPEN'))
socket.on('message', (message) => console.log('MSG: ' + message))
socket.on('close', (code, reason) => console.log('CLOSE'))
socket.on('error', (message) => console.log('ERROR'))
socket.send('message')
socket.ping() // send WS PING frame
socket.pong() // send WS PONG frame (unsolicited)
socket.close()
}, err => {
console.log('Socket creation failed!')
});