diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-07 14:40:45 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-07 14:40:45 +1200 |
commit | 06c4ee387669b1ccb5aa3928fbaa7c3650bfb7fa (patch) | |
tree | 76b9320815b51c242513a036c1fd2fc37082d5c7 /modern/src/SocketContoller.js | |
parent | 58b66d9a09d3128f96f8c42f0e271a2323581143 (diff) | |
download | trackermap-web-06c4ee387669b1ccb5aa3928fbaa7c3650bfb7fa.tar.gz trackermap-web-06c4ee387669b1ccb5aa3928fbaa7c3650bfb7fa.tar.bz2 trackermap-web-06c4ee387669b1ccb5aa3928fbaa7c3650bfb7fa.zip |
Implement WebSocket connection
Diffstat (limited to 'modern/src/SocketContoller.js')
-rw-r--r-- | modern/src/SocketContoller.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/modern/src/SocketContoller.js b/modern/src/SocketContoller.js new file mode 100644 index 00000000..c07fcff0 --- /dev/null +++ b/modern/src/SocketContoller.js @@ -0,0 +1,30 @@ +import { Component } from 'react'; + +class SocketController extends Component { + connectSocket() { + const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + const socket = new WebSocket(protocol + '//' + window.location.host + '/api/socket'); + + socket.onclose = () => { + setTimeout(() => this.connectSocket(), 60 * 1000); + }; + + socket.onmessage = (event) => { + const data = JSON.parse(event.data); + if (data.positions) { + // TODO update positions + console.log(data.positions); + } + } + } + + componentDidMount() { + this.connectSocket(); + } + + render() { + return null; + } +} + +export default SocketController; |