You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.3KB

  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. addEventListener('resize', () => m.redraw())
  16. const wire = (message) => State.websocket.send(JSON.stringify(message))
  17. const signal = (message) => dispatchEvent(new CustomEvent(message.kind, {detail: message}))
  18. const listen = (kind, handler) => {
  19. addEventListener(kind, handler)
  20. }
  21. listen('login', ({detail}) => {
  22. State.username = detail.value
  23. State.messages = []
  24. })
  25. listen('logout', ({detail}) => {
  26. State.online = []
  27. })
  28. listen('state', ({detail}) => {
  29. delete detail.ts
  30. delete detail.kind
  31. Object.assign(State, detail)
  32. })
  33. const doNotLog = new Set(['login', 'state', 'post', 'peerInfo', 'join', 'leave'])
  34. /*
  35. *
  36. * ALERTS
  37. *
  38. */
  39. State.unseen = 0
  40. listen('beep', () => {State.unseen += !document.hasFocus(); updateTitle()})
  41. listen('focus', () => {State.unseen = 0; updateTitle()})
  42. const updateTitle = () => {
  43. document.title = location.href.split('//')[1] + (State.unseen ? ` (${State.unseen})` : ``)
  44. }
  45. /*
  46. *
  47. * UTILS
  48. *
  49. */
  50. const autoFocus = (vnode) => {
  51. vnode.dom.focus()
  52. }
  53. /*
  54. *
  55. * WEBSOCKET
  56. *
  57. */
  58. const connect = (username) => {
  59. const wsUrl = location.href.replace('http', 'ws')
  60. State.websocket = new WebSocket(wsUrl)
  61. State.websocket.onopen = (e) => {
  62. wire({kind: 'login', value: username})
  63. }
  64. State.websocket.onmessage = (e) => {
  65. const message = JSON.parse(e.data)
  66. if(message.online) {
  67. const difference = (l1, l2) => l1.filter(u => !l2.includes(u))
  68. difference(message.online, State.online).forEach(username => {
  69. signal({kind: 'post', ts: message.ts, value: `${username} joined`})
  70. signal({kind: 'join', username: username})
  71. })
  72. difference(State.online, message.online).forEach(username => {
  73. signal({kind: 'post', ts: message.ts, value: `${username} left`})
  74. signal({kind: 'leave', username: username})
  75. })
  76. }
  77. if(!doNotLog.has(message.kind)) {
  78. console.log(message)
  79. }
  80. signal(message)
  81. m.redraw()
  82. }
  83. State.websocket.onclose = (e) => {
  84. State.online.forEach(signalPeerStop)
  85. if(!e.wasClean) {
  86. setTimeout(connect, 1000, username)
  87. }
  88. m.redraw()
  89. }
  90. }
  91. /*
  92. *
  93. * BASE
  94. *
  95. */
  96. const Base = {
  97. oncreate: () => {
  98. if(localStorage.username) {
  99. connect(localStorage.username)
  100. }
  101. },
  102. sendLogin: (e) => {
  103. e.preventDefault()
  104. const username = e.target.username.value
  105. localStorage.username = username
  106. connect(username)
  107. },
  108. sendLogout: (e) => {
  109. e.preventDefault()
  110. wire({kind: 'logout'})
  111. signal({kind: 'logout'})
  112. },
  113. view() {
  114. const attrs = {
  115. oncreate: autoFocus,
  116. name: 'username',
  117. autocomplete: 'off',
  118. value: localStorage.username,
  119. }
  120. const mainStyle = {
  121. position: 'fixed',
  122. width: '100%',
  123. display: 'grid',
  124. gridTemplateRows: 'auto 1fr',
  125. height: window.innerHeight + 'px',
  126. overflow: 'hidden',
  127. }
  128. const headerStyle = {
  129. display: 'grid',
  130. gridAutoFlow: 'column',
  131. justifyItems: 'start',
  132. marginRight: 'auto',
  133. }
  134. return m('main', {style: mainStyle},
  135. m('header', {style: headerStyle},
  136. State.isConnected ? null : m('form.login',
  137. {onsubmit: Base.sendLogin},
  138. m('input', attrs),
  139. m('button', 'Login'),
  140. ),
  141. State.isConnected ? [
  142. m('button', {onclick: Base.sendLogout}, 'logout'),
  143. m('button', {onclick: () => navigator.clipboard.writeText(location)}, 'copy url'),
  144. ] : null,
  145. State.isConnected ? m(VideoConfig) : null,
  146. State.isConnected ? m(ChatConfig) : null,
  147. m('span.error', State.info),
  148. ),
  149. State.isConnected ? m(StreamContainer) : null,
  150. State.isConnected ? m(Chat) : null,
  151. )
  152. },
  153. }
  154. m.mount(document.body, Base)