SocketIoContainer.js 840 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 SocketIoContainer extends Container {
  8. constructor(appContainer, namespace) {
  9. super();
  10. this.appContainer = appContainer;
  11. this.appContainer.registerContainer(this);
  12. const ns = namespace || '/';
  13. this.socket = io(ns, {
  14. transports: ['websocket'],
  15. });
  16. this.socketClientId = Math.floor(Math.random() * 100000);
  17. this.state = {
  18. };
  19. }
  20. /**
  21. * Workaround for the mangling in production build to break constructor.name
  22. */
  23. static getClassName() {
  24. return 'SocketIoContainer';
  25. }
  26. getSocket() {
  27. return this.socket;
  28. }
  29. getSocketClientId() {
  30. return this.socketClientId;
  31. }
  32. }