aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketContoller.js
blob: fe686ed8a7769596123bd2295b39e000124c28ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Component } from 'react';
import { connect } from 'react-redux';
import { updatePositions } from './actions';

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) {
        this.props.dispatch(updatePositions(data.positions));
      }
    }
  }

  componentDidMount() {
    this.connectSocket();
  }

  render() {
    return null;
  }
}

export default connect()(SocketController);