A sample application that keeps a permanent WebSocket connection in an Android bound Service.
When a message is received from the websocket, an event is sent via LocalBroadcastManager.
It handles network configuration changes using a receiver to stop/start the websocket depending on the network state.
The sheep in the current activity will start to bleat when a message is received from the websocket.
- First, use NodeJS to launch the server:
$ npm install
$ node main
- You can then access the websocket through:
http://localhost:3000/ws/
- Then, modify the
com.nilhcem.websockets.WebSocketsService.WS_URL
constant to specify the server IP:
private static final String WS_URL = "ws://SERVER_IP:3000/ws/websocket";
- Compile, deploy and run the application:
$ ./gradlew clean installDebug
- Finally, send a POST request to
http://SERVER_IP:3000/dispatchMessage
, the websocket will send the message to all the connected clients. You can use the awesome rest-client tool to assist you in this task.
- If the sheep bleats, that's a success (you can also see the received message in the device logs)