WebsocketContainer.js 756 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Container } from 'unstated';
  2. import io from 'socket.io-client';
  3. /**
  4. * Service container related to options for WebSocket
  5. * @extends {Container} unstated Container
  6. */
  7. export default class WebsocketContainer extends Container {
  8. constructor(appContainer) {
  9. super();
  10. this.appContainer = appContainer;
  11. this.appContainer.registerContainer(this);
  12. this.socket = io();
  13. this.socketClientId = Math.floor(Math.random() * 100000);
  14. this.state = {
  15. };
  16. }
  17. /**
  18. * Workaround for the mangling in production build to break constructor.name
  19. */
  20. static getClassName() {
  21. return 'WebsocketContainer';
  22. }
  23. getWebSocket() {
  24. return this.socket;
  25. }
  26. getSocketClientId() {
  27. return this.socketClientId;
  28. }
  29. }