選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pico.js 4.2KB

5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. display: 'grid',
  122. gridTemplateRows: 'auto 1fr',
  123. height: '100vh',
  124. overflow: 'hidden',
  125. }
  126. const headerStyle = {
  127. display: 'grid',
  128. gridAutoFlow: 'column',
  129. justifyItems: 'start',
  130. marginRight: 'auto',
  131. }
  132. return m('main', {style: mainStyle},
  133. m('header', {style: headerStyle},
  134. State.isConnected ? null : m('form.login',
  135. {onsubmit: Base.sendLogin},
  136. m('input', attrs),
  137. m('button', 'Login'),
  138. ),
  139. State.isConnected ? m('form.logout',
  140. {onsubmit: Base.sendLogout},
  141. m('button', 'Logout'),
  142. m('input[readonly]', {value: location}),
  143. ) : null,
  144. State.isConnected ? m(VideoConfig) : null,
  145. State.isConnected ? m(ChatConfig) : null,
  146. m('span.error', State.info),
  147. ),
  148. State.isConnected ? m(StreamContainer) : null,
  149. State.isConnected ? m(Chat) : null,
  150. )
  151. },
  152. }
  153. m.mount(document.body, Base)