Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今更 WebSocket を学んでみた話 #sacss

今更 WebSocket を学んでみた話 #sacss

Kihara, Takuya

February 12, 2020
Tweet

More Decks by Kihara, Takuya

Other Decks in Technology

Transcript

  1. !UBDDL 4B$44WPM TBDTT • ໊લ ໦ݪ ୎໵ / @tacck •

    ॴଐ גࣜձࣾϊʔεσΟςʔϧ
 ։ൃDivision ٕज़ਪਐGroup / Leader • ओ࠵ • ΏΔWebษڧձ@ࡳຈ • εϚʔτεϐʔΧʔͰ༡΅͏ձ@ࡳຈ • ޷͖ͳϑΟΪϡΞεέʔτͷٕ εϓϨουɾΠʔάϧ ୭ PHP Conference Hokkaido 2019
  2. !UBDDL 4B$44WPM TBDTT • WebΫϥΠΞϯτ - WebαʔόؒͰ࢖͑Δɺ
 ͓खܰͳ૒ํ޲௨৴ͷن֨ɻ • Webϒϥ΢β؍఺Ͱ͸ɺJavaScriptͷ


    ΠϕϯτϕʔεͰ࣮૷Մೳɻ
 → ΠϕϯττϦΨʔͰDOMߋ৽͕Ͱ͖Δ
 →→ νϟοτͳͲͷϦΞϧλΠϜΞϓϦ͕࡞Γ΍͍͢ɻ 8FC4PDLFUͱ͸
  3. !UBDDL 4B$44WPM TBDTT • ϒϥ΢β͔Βαʔόʔʹݺͼ͔͚ͯ௨৴։࢝ (TCP) • ϒϥ΢βܖػͰαʔόʔͱσʔλૹड৴ (HTTP) •

    ΍ΓͱΓ͕ऴΘͬͯ΋গ͠͸ܨ͍ͩ·· (HTTP/1.1 KeepAlive) • ͠͹Β͘ܦͭͱ௨৴ऴྃ )551 ͷجຊ
  4. !UBDDL 4B$44WPM TBDTT • ͠͹Β͘ܦͬͨ͋ͱʹ௨৴͠Α͏ͱ͢Δͱɺ
 ࠷ॳͷʮѫࡰʯ෦෼͔Β࢝·Δɻ • ͕͔͔࣌ؒΔɻ • ௨৴։࢝͸Ϛγϯతʹ΋ίετ͕ߴ͍ɻ

    • ʮΫϥΠΞϯτ͔Βʯ͔͠σʔλૹड৴։࢝Ͱ͖ͳ͍ɻ • αʔόʔ͔Β৘ใΛ఻͍͑ͨɺͱ͍͏γεςϜΛ
 ࣮ݱͰ͖ͳ͍ɻ )551 ͷ՝୊
  5. !UBDDL 4B$44WPM TBDTT • ͠͹Β͘ܦͬͨ͋ͱʹ௨৴͠Α͏ͱ͢Δͱɺ
 ࠷ॳͷʮѫࡰʯ෦෼͔Β࢝·Δɻ • ͕͔͔࣌ؒΔɻ • ௨৴։࢝͸Ϛγϯతʹ΋ίετ͕ߴ͍ɻ

    • ʮΫϥΠΞϯτ͔Βʯ͔͠σʔλૹड৴։࢝Ͱ͖ͳ͍ɻ • αʔόʔ͔Β৘ใΛ఻͍͑ͨɺͱ͍͏γεςϜΛ
 ࣮ݱͰ͖ͳ͍ɻ • HTTP/2.0 ͔Βͷ࿩͸ɺࠓճׂѪɻ )551 ͷ՝୊
  6. !UBDDL 4B$44WPM TBDTT • WebSocket Λ࢖͏ͱɺ
 αʔόʔܖػͰσʔλૹ৴Λߦͳ͑Δɻ
 (ैདྷͷ HTTP ͚ͩͰ͸Ͱ͖ͳ͔ͬͨɻ)

    • ΫϥΠΞϯτ͸ɺ JavaScript ͷΠϕϯτͱͯ͠
 ॲཧͰ͖ΔͷͰɺ࣮૷೉қ౓͸ͦ͜·Ͱߴ͘ͳ͍ɻ • αʔόʔͷ࣮૷΋ɺجຊ͸ͦ͜·Ͱ೉͘͠ͳ͍ɻ 8FC4PDLFUͰ՝୊ղܾ
  7. !UBDDL 4B$44WPM TBDTT • Webαʔό (Node.js) • “post”ϝοηʔδΛड͚औͬͨΒɺ
 idΛ֬ೝͯ͠Χ΢ϯτΞοϓɻ •

    Χ΢ϯτΞοϓޙʹɺ“selections”ϝοηʔδΛ
 શΫϥΠΞϯτʹ݁Ռૹ৴ɻ ؆୯ͳϦΞϧλΠϜ౤ථΞϓϦ
  8. !UBDDL 4B$44WPM TBDTT socket.on('message', message => { console.log(message) const obj

    = JSON.parse(message) const type = obj.message const data = obj.data if (type === 'post') { selections[data.id].count++ } wss.clients.forEach(client => { client.send(JSON.stringify( { message: 'selections', data: selections } )) }) })
  9. !UBDDL +BWB%PͰ͠ΐ͏ KBWBEP mounted() { this.ws = new WebSocket('wss://tacck-ws.herokuapp.com/') const

    self = this this.ws.addEventListener('message', message => { const obj = JSON.parse(message.data) const type = obj.message const data = obj.data if (type === 'selections') { self.selections = data } }) }, methods: { send(id) { this.ws.send(JSON.stringify( { message: 'post', data: { id: id } } )) } }
  10. !UBDDL 4B$44WPM TBDTT • ैདྷͷ HTTP/1.0,1.1 Ͱ͸
 αʔόʔܖػͰσʔλૹड৴͕Ͱ͖ͳ͔ͬͨɻ • WebSocket

    ͰͦΕΛղܾՄೳɻ • ΑΓൃలͨ͠࢓૊Έ͸ HTTP/2.0 Ҏ߱ʹ͋Δ͕ɺ
 ͦΕ͸·ͨผͷ͓࿩ɻ ·ͱΊ