From 06c4ee387669b1ccb5aa3928fbaa7c3650bfb7fa Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 7 Sep 2018 14:40:45 +1200 Subject: Implement WebSocket connection --- modern/src/SocketContoller.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 modern/src/SocketContoller.js (limited to 'modern/src/SocketContoller.js') diff --git a/modern/src/SocketContoller.js b/modern/src/SocketContoller.js new file mode 100644 index 0000000..c07fcff --- /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; -- cgit v1.2.3