Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

187 lines
4.9KB

  1. const State = Object.seal({
  2. username: null,
  3. websocket: null,
  4. online: [],
  5. get isConnected() {
  6. return State.websocket && State.websocket.readyState === 1
  7. },
  8. })
  9. const params = (new URL(document.location)).searchParams
  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. })
  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. const doNotLog = new Set(['login', 'state', 'post', 'peerInfo'])
  33. /*
  34. *
  35. * UTILS
  36. *
  37. */
  38. const autoFocus = (vnode) => {
  39. vnode.dom.focus()
  40. }
  41. /*
  42. *
  43. * WEBSOCKET
  44. *
  45. */
  46. const connect = (username) => {
  47. const wsUrl = location.href.replace('http', 'ws')
  48. State.websocket = new WebSocket(wsUrl)
  49. State.websocket.onopen = (e) => {
  50. wire({kind: 'login', value: username})
  51. }
  52. State.websocket.onmessage = (e) => {
  53. const message = JSON.parse(e.data)
  54. if(message.online) {
  55. const difference = (l1, l2) => l1.filter(u => !l2.includes(u))
  56. difference(message.online, State.online).forEach(username => {
  57. if(username === State.username) return
  58. signal({kind: 'post', ts: message.ts, value: `${username} joined`})
  59. })
  60. difference(State.online, message.online).forEach(username => {
  61. if(username === State.username) return
  62. signal({kind: 'post', ts: message.ts, value: `${username} left`})
  63. })
  64. }
  65. if(!doNotLog.has(message.kind)) {
  66. console.log(message)
  67. }
  68. signal(message)
  69. m.redraw()
  70. }
  71. State.websocket.onclose = (e) => {
  72. State.online.forEach(signalPeerStop)
  73. if(!e.wasClean) {
  74. setTimeout(connect, 1000, username)
  75. }
  76. m.redraw()
  77. }
  78. }
  79. /*
  80. *
  81. * BASE
  82. *
  83. */
  84. const Settings = {
  85. get(key) {
  86. try {
  87. return JSON.parse(localStorage.getItem(key))
  88. }
  89. catch(error) {
  90. return null
  91. }
  92. },
  93. set(key, value) {
  94. localStorage.setItem(key, JSON.stringify(value))
  95. },
  96. multiField: ([key, options]) => {
  97. let current = Settings.get(key)
  98. if(!options.includes(current)) {
  99. Settings.set(key, options[0])
  100. }
  101. return m('.field',
  102. m('label', key),
  103. options.map(value => {
  104. const style = {
  105. fontWeight: Settings.get(key) == value ? 'bold' : 'unset'
  106. }
  107. const onclick = () => Settings.set(key, value)
  108. return m('button', {style, onclick}, `${value}`)
  109. })
  110. )
  111. },
  112. view() {
  113. return m('.settings',
  114. Object.entries({
  115. blackBars: [true, false],
  116. }).map(Settings.multiField)
  117. )
  118. },
  119. }
  120. const Base = {
  121. oncreate: () => {
  122. const randomName = ('' + Math.random()).substring(2)
  123. connect(localStorage.username || randomName)
  124. },
  125. sendLogin: (e) => {
  126. e.preventDefault()
  127. const username = e.target.username.value
  128. localStorage.username = username
  129. connect(username)
  130. },
  131. sendLogout: (e) => {
  132. e.preventDefault()
  133. wire({kind: 'logout'})
  134. signal({kind: 'logout'})
  135. },
  136. view() {
  137. const attrs = {
  138. oncreate: autoFocus,
  139. name: 'username',
  140. autocomplete: 'off',
  141. value: localStorage.username,
  142. }
  143. const mainStyle = {
  144. position: 'fixed',
  145. width: '100%',
  146. display: 'grid',
  147. gridTemplateRows: 'auto 1fr',
  148. height: window.innerHeight + 'px',
  149. overflow: 'hidden',
  150. }
  151. const headerStyle = {
  152. display: 'grid',
  153. gridAutoFlow: 'column',
  154. justifyItems: 'start',
  155. marginRight: 'auto',
  156. }
  157. return m('main', {style: mainStyle},
  158. m('header', {style: headerStyle},
  159. State.isConnected ? [
  160. m('button', {onclick: Base.sendLogout}, 'settings'),
  161. m(VideoConfig),
  162. m(ChatConfig),
  163. ] : [
  164. m('form.login',
  165. {onsubmit: Base.sendLogin},
  166. m('input', attrs),
  167. m('button', 're-join'),
  168. ),
  169. ],
  170. m('span.error', State.info),
  171. ),
  172. State.isConnected ? [
  173. m(StreamContainer),
  174. m(Chat),
  175. ] : m(Settings),
  176. )
  177. },
  178. }
  179. m.mount(document.body, Base)