Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

153 linhas
3.7KB

  1. const State = Object.seal({
  2. username: null,
  3. websocket: null,
  4. online: [],
  5. messages: [],
  6. get isConnected() {
  7. return State.websocket && State.websocket.readyState === 1
  8. },
  9. })
  10. /*
  11. *
  12. * SIGNALING
  13. *
  14. */
  15. const wire = (message) => State.websocket.send(JSON.stringify(message))
  16. const signal = (message) => dispatchEvent(new CustomEvent(message.kind, {detail: message}))
  17. const listen = (kind, handler) => {
  18. addEventListener(kind, handler)
  19. }
  20. listen('login', ({detail}) => {
  21. State.username = detail.value
  22. State.messages = []
  23. })
  24. listen('logout', ({detail}) => {
  25. State.online = []
  26. })
  27. listen('state', ({detail}) => {
  28. delete detail.ts
  29. delete detail.kind
  30. Object.assign(State, detail)
  31. })
  32. listen('post', ({detail}) => {
  33. State.messages.push(detail)
  34. m.redraw()
  35. })
  36. const doNotLog = new Set(['login', 'state', 'post', 'peerInfo', 'join', 'leave'])
  37. /*
  38. *
  39. * ALERTS
  40. *
  41. */
  42. State.unseen = 0
  43. listen('post', () => {State.unseen += !document.hasFocus(); updateTitle()})
  44. listen('focus', () => {State.unseen = 0; updateTitle()})
  45. const updateTitle = () => {
  46. document.title = location.href.split('//')[1] + (State.unseen ? ` (${State.unseen})` : ``)
  47. }
  48. /*
  49. *
  50. * UTILS
  51. *
  52. */
  53. const autoFocus = (vnode) => {
  54. vnode.dom.focus()
  55. }
  56. /*
  57. *
  58. * BASE
  59. *
  60. */
  61. const Base = {
  62. sendLogin: (e) => {
  63. e.preventDefault()
  64. const username = e.target.username.value
  65. localStorage.username = username
  66. connect(username)
  67. },
  68. sendLogout: (e) => {
  69. e.preventDefault()
  70. wire({kind: 'logout'})
  71. signal({kind: 'logout'})
  72. },
  73. view() {
  74. const attrs = {
  75. oncreate: autoFocus,
  76. name: 'username',
  77. autocomplete: 'off',
  78. value: localStorage.username,
  79. }
  80. const style = {
  81. display: 'inline',
  82. }
  83. return m('main',
  84. m('span.login-container',
  85. State.isConnected ? null : m('form.login',
  86. {style, onsubmit: Base.sendLogin},
  87. m('input', attrs),
  88. m('button', 'Login'),
  89. ),
  90. State.isConnected ? m('form.logout',
  91. {style, onsubmit: Base.sendLogout},
  92. m('button', 'Logout'),
  93. m('input[readonly]', {value: location}),
  94. ) : null,
  95. m('span.error', State.info),
  96. ),
  97. State.isConnected ? m(StreamContainer) : null,
  98. )
  99. },
  100. }
  101. m.mount(document.body, Base)
  102. /*
  103. *
  104. * WEBSOCKET
  105. *
  106. */
  107. const connect = (username) => {
  108. const wsUrl = location.href.replace('http', 'ws')
  109. State.websocket = new WebSocket(wsUrl)
  110. State.websocket.onopen = (e) => {
  111. wire({kind: 'login', value: username})
  112. }
  113. State.websocket.onmessage = (e) => {
  114. const message = JSON.parse(e.data)
  115. if(message.online) {
  116. const difference = (l1, l2) => l1.filter(u => !l2.includes(u))
  117. difference(message.online, State.online).forEach(username => {
  118. signal({kind: 'post', ts: message.ts, value: `${username} joined`})
  119. signal({kind: 'join', username: username})
  120. })
  121. difference(State.online, message.online).forEach(username => {
  122. signal({kind: 'post', ts: message.ts, value: `${username} left`})
  123. signal({kind: 'leave', username: username})
  124. })
  125. }
  126. if(!doNotLog.has(message.kind)) {
  127. console.log('@', message)
  128. }
  129. signal(message)
  130. m.redraw()
  131. }
  132. State.websocket.onclose = (e) => {
  133. State.online.forEach(signalPeerStop)
  134. if(!e.wasClean) {
  135. setTimeout(connect, 1000, username)
  136. }
  137. m.redraw()
  138. }
  139. }
  140. if(localStorage.username) {
  141. connect(localStorage.username)
  142. }