App.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { Component } from 'react';
  2. import {connect} from 'react-redux';
  3. import {init, tick} from '../action-creators';
  4. import ActionTypes from '../action-types';
  5. import LineChart from './LineChart';
  6. const styles = {
  7. container: {
  8. backgroundSize: 'cover',
  9. display: 'flex',
  10. padding: 32,
  11. flexDirection: 'column'
  12. },
  13. chartContainer: {
  14. }
  15. }
  16. const WS_URL = 'ws://localhost:8080/ws';
  17. class App extends Component {
  18. constructor(props) {
  19. super();
  20. const ws = new WebSocket(WS_URL);
  21. ws.onerror = err => console.log(err);
  22. ws.onopen = event => console.log(event);
  23. ws.onclose = event => console.log(event);
  24. ws.onmessage = event => {
  25. const message = JSON.parse(event.data);
  26. console.log(message);
  27. props.dispatch(message);
  28. };
  29. this.state = {ws};
  30. }
  31. render() {
  32. const {ram, nClients} = this.props;
  33. return (
  34. <div style={styles.container}>
  35. <LineChart
  36. data={ram.get('data').toArray()}
  37. domain={[ram.get('min'), ram.get('max')]}
  38. format="bytes" />
  39. <LineChart
  40. data={nClients.get('data').toArray()}
  41. domain={[nClients.get('min'), nClients.get('max')]}
  42. format="number" />
  43. </div>
  44. );
  45. }
  46. }
  47. const mapStateToProps = state => ({
  48. ram: state.get('ram'),
  49. nClients: state.get('nClients')
  50. });
  51. export default connect(mapStateToProps)(App);