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

pico.js 3.7KB

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年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. return m('main',
  81. m('.login-container',
  82. m('form.login' + (State.isConnected ? '.hidden' : ''),
  83. {onsubmit: Base.sendLogin},
  84. m('input', attrs),
  85. m('button', 'Login'),
  86. ),
  87. m('form.logout' + (State.isConnected ? '' : '.hidden'),
  88. {onsubmit: Base.sendLogout},
  89. m('button', 'Logout'),
  90. m('input[readonly]', {value: location}),
  91. ),
  92. m('.error', State.info),
  93. ),
  94. State.isConnected ? m(StreamContainer) : null,
  95. )
  96. },
  97. }
  98. m.mount(document.body, Base)
  99. /*
  100. *
  101. * WEBSOCKET
  102. *
  103. */
  104. const connect = (username) => {
  105. const wsUrl = location.href.replace('http', 'ws')
  106. State.websocket = new WebSocket(wsUrl)
  107. State.websocket.onopen = (e) => {
  108. wire({kind: 'login', value: username})
  109. }
  110. State.websocket.onmessage = (e) => {
  111. const message = JSON.parse(e.data)
  112. if(message.online) {
  113. const difference = (l1, l2) => l1.filter(u => !l2.includes(u))
  114. difference(message.online, State.online).forEach(username => {
  115. signal({kind: 'post', ts: message.ts, value: `${username} joined`})
  116. signal({kind: 'join', username: username})
  117. })
  118. difference(State.online, message.online).forEach(username => {
  119. signal({kind: 'post', ts: message.ts, value: `${username} left`})
  120. signal({kind: 'leave', username: username})
  121. })
  122. }
  123. if(!doNotLog.has(message.kind)) {
  124. console.log('@', message)
  125. }
  126. signal(message)
  127. m.redraw()
  128. }
  129. State.websocket.onclose = (e) => {
  130. State.online.forEach(signalPeerStop)
  131. if(!e.wasClean) {
  132. setTimeout(connect, 1000, username)
  133. }
  134. m.redraw()
  135. }
  136. }
  137. if(localStorage.username) {
  138. connect(localStorage.username)
  139. }