Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~...
Search
shiro seike
PRO
December 22, 2024
Programming
2
1.2k
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
PHP Conference Japan 2024
https://phpcon.php.gr.jp/2024
shiro seike
PRO
December 22, 2024
Tweet
Share
More Decks by shiro seike
See All by shiro seike
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
830
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
420
地方のPHPerもクラウドを使う理由 ~コストの最適化とチームに向き合う~ / Why even local PHPers use the cloud ~optimize costs and face the team
seike460
PRO
0
70
OpenTelemetryで始めるベンダーフリーなobservability / Vendor-free observability starting with OpenTelemetry
seike460
PRO
0
190
AIコーディングの本質は“コード“ではなく“構造“だった / The essence of AI coding is not “code” but "structure
seike460
PRO
2
1.2k
OpenTelemetryを活用したObservability入門 / Introduction to Observability with OpenTelemetry
seike460
PRO
1
720
Amazon Q Developer Proで効率化するAPI開発入門
seike460
PRO
0
380
実行委員長が振り返るBacklogWorldの裏側と運営Backlog術 / The chairperson of the executive committee looks back on the backstage of BacklogWorld and the art of managing Backlog.
seike460
PRO
0
9
Amazon Aurora DSQLパフォーマンスチェック / Amazon Aurora DSQL Performance Check
seike460
PRO
0
18
Other Decks in Programming
See All in Programming
AIエージェント開発、DevOps and LLMOps
ymd65536
1
340
CSC305 Summer Lecture 05
javiergs
PRO
0
110
なぜ今、Terraformの本を書いたのか? - 著者陣に聞く!『Terraformではじめる実践IaC』登壇資料
fufuhu
4
650
CSC305 Summer Lecture 04
javiergs
PRO
1
100
AIレビュアーをスケールさせるには / Scaling AI Reviewers
technuma
2
230
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
140
マイコンでもRustのtestがしたい その2/KernelVM Tokyo 18
tnishinaga
2
2.3k
「リーダーは意思決定する人」って本当?~ 学びを現場で活かす、リーダー4ヶ月目の試行錯誤 ~
marina1017
0
240
tool ディレクティブを導入してみた感想
sgash708
1
150
ライブ配信サービスの インフラのジレンマ -マルチクラウドに至ったワケ-
mirrativ
2
260
新世界の理解
koriym
0
140
ワープロって実は計算機で
pepepper
2
1.4k
Featured
See All Featured
Music & Morning Musume
bryan
46
6.7k
Agile that works and the tools we love
rasmusluckow
329
21k
How GitHub (no longer) Works
holman
315
140k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
110
20k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.4k
A better future with KSS
kneath
239
17k
Fireside Chat
paigeccino
39
3.6k
Unsuck your backbone
ammeep
671
58k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Transcript
©Fusic Co., Ltd. 1 PHPͰ࡞ΔWebSocketαʔόʔ ~ϦΞΫςΟϒͳΞϓϦέʔγϣϯΛΔͨΊʹ~ 2024.12.22 @seike460 PHP Conference
Japan 2024
©Fusic Co., Ltd. 2 ਗ਼Ո ࢙ @seike460 AWS Community Builder
Serverless AWS User Group Leaders Community Organizer ɹFukuoka.php ɹFukuoka.go ɹJAWS-UG Fukuoka ɹServerless Meetup Fukuoka ɹCloudflare Meetup Fukuoka ɹJP_Stripes Fukuoka ࣗݾհ ͡Ίʹ גࣜձࣾFusic ϓϦϯγύϧΤϯδχΞ/ΤόϯδΣϦετ
©Fusic Co., Ltd. 3 CONTENTS ࣍ 1. WebSocketϋϯυγΣΠΫ 2. WebSocketϑϨʔϜަ
3. JavaScriptଆͷ࣮ 4. PHPଆͷ࣮ 5. σϞ 6. ·ͱΊ
©Fusic Co., Ltd. 4 WebSocketϋϯυγΣΠΫ 1
©Fusic Co., Ltd. 5 WebSocketͷ֓ཁ ํ௨৴ 'VMM%VQMFY HTTP / 01
ίωΫγϣϯͷҡ࣋ 一 立 立 02 Φʔόʔϔου HTTP 高 用 03 5
©Fusic Co., Ltd. 6 WebSocketϋϯυγΣΠΫ 6 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 HTTP௨৴ͷҰͭͰ͋ΔͷͰɺ HTTPϔομʔΛ֬ೝ
©Fusic Co., Ltd. 7 WebSocketϋϯυγΣΠΫ 7 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 GETͰɺ/chatʹɺHTTP/1.1Ͱଓ Hostͷexample.comʹରͯ͠8000൪ϙʔτͰଓ
©Fusic Co., Ltd. 8 WebSocketϋϯυγΣΠΫ 8 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 HTTP͔ΒWebSocketͷϓϩτίϧΓସ͑Λཁٻ ʮΓସ͍͑ͨϓϩτίϧͷछྨʯΛࢦఆ
©Fusic Co., Ltd. 9 WebSocketϋϯυγΣΠΫ 9 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 ϓϩτίϧͷΞοϓάϨʔυΛࣔ͢ ʮϓϩτίϧΛΓସ͑ΔҙࢥʯΛ͑Δ Connection: closeͳͲ͋ΔʢଓΛऴྃ͢Δҙࢥʣ
©Fusic Co., Ltd. 10 WebSocketϋϯυγΣΠΫ 10 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 16όΠτҎ্ΛBase64Τϯίʔυͨ͠ͷ ΫϥΠΞϯτ͕͜ͷΛࣗ༝ʹੜՄೳ ੜنଇ͕ఆΊΒΕ͍ͯΔΘ͚Ͱ͋Γ·ͤΜ
©Fusic Co., Ltd. 11 WebSocketϋϯυγΣΠΫ 11 Client server Request GET
/chat HTTP/ 1 . 1 Host: example.com: 80 00 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub 2 5 jZQ== Sec-WebSocket-Version: 13 WebSocketͷVersionΛࣔ͠·͢ αʔόʔ͕ड৴ͨ͠σʔλΛղऍग़དྷΔ͔֬ೝ ղऍͰ͖ͳ͍߹ଓΛ҆શʹऴྃ͢Δ
©Fusic Co., Ltd. 12 WebSocketϋϯυγΣΠΫ 12 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= RequestΛड͚ͯɺServer͕ResponseΛฦ٫
©Fusic Co., Ltd. 13 WebSocketϋϯυγΣΠΫ 13 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= 101 Switching Protocolsͱͯ͠ɺ Clientͷཁٻͨ͠ProtocolʹΓସ͑ΔࣄΛྃঝɺฦ
©Fusic Co., Ltd. 14 WebSocketϋϯυγΣΠΫ 14 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= Requestͱಉ༷ WebSocketʹUpgrade͢Δ͜ͱΛฦ
©Fusic Co., Ltd. 15 WebSocketϋϯυγΣΠΫ 15 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= ᶃΫϥΠΞϯτ͕ Sec-WebSocket-Key ͱͯ͠ૹ৴
©Fusic Co., Ltd. 16 WebSocketϋϯυγΣΠΫ 16 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= ᶃΫϥΠΞϯτ͕ Sec-WebSocket-Key ͱͯ͠ૹ৴ ᶄWebSocket༷Ͱఆٛ͞ΕͨݻఆGUIDΛ࿈݁ 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 ↓ dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
©Fusic Co., Ltd. 17 WebSocketϋϯυγΣΠΫ 17 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= ᶃΫϥΠΞϯτ͕ Sec-WebSocket-Key ͱͯ͠ૹ৴ ᶄWebSocket༷Ͱఆٛ͞ΕͨݻఆGUIDΛ࿈݁ ᶅ࿈݁ͨ͠จࣈྻΛSHA-1ͰϋογϡԽ b"\xB3z\xCB0\x98O\x16\x90\xF6F\x18\xCE\x38Y\x16\xB2\x94\xEA\x82\x9F"
©Fusic Co., Ltd. 18 WebSocketϋϯυγΣΠΫ 18 Client server Response Request
HTTP/ 1 . 1 1 01 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s 3 pPLMBiTxaQ 9 kYGzzhZRbK+xOo= ᶃΫϥΠΞϯτ͕ Sec-WebSocket-Key ͱͯ͠ૹ৴ ᶄWebSocket༷Ͱఆٛ͞ΕͨݻఆGUIDΛ࿈݁ ᶅ࿈݁ͨ͠จࣈྻΛSHA-1ͰϋογϡԽ ᶆϋογϡԽ͞Εͨ݁ՌΛBase64ͰΤϯίʔυ s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
©Fusic Co., Ltd. 19 WebSocketϑϨʔϜͷߏ 2
©Fusic Co., Ltd. 20 WebSocketϑϨʔϜ 20 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 - Socket௨৴ - ωοτϫʔΫ্ͰσʔλΛૹड৴͢ΔΈ ΫϥΠΞϯτͱαʔόʔؒͷํ௨৴Λ࣮ݱ - WebSocketϑϨʔϜ - SocketͷதͰWebSocketϑϨʔϜΛަ ϔομʔϖΠϩʔυʢ࣮ࡍͷσʔλʣΛ௨৴
©Fusic Co., Ltd. 21 WebSocketϑϨʔϜ 21 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 ϋϯυγΣΠΫྃޙɺΫϥΠΞϯτͱαʔόʔ ্هͷΑ͏ͳόΠφϦσʔλͷަΛߦ͍·͢ ϝοηʔδ͕ "Hello" ͷ߹ 81: FIN=1, OPCODE=1 (ςΩετϑϨʔϜ) 85: MASK=1, ϖΠϩʔυ=5 37 FA 21 3D: ϚεΫΩʔ 7F 9F 4D 51 58: ϚεΫ͞Εͨσʔλ
©Fusic Co., Ltd. 22 WebSocketϑϨʔϜ 22 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 81: FIN=1, OPCODE=1 (ςΩετϑϨʔϜ) FIN=1ͷ߹ɺ͜ͷϑϨʔϜ͕ϝοηʔδͷ࠷ޙ FIN=0ͷ߹ɺ·ͩଓ͖ͷϑϨʔϜ͕͋ΔࣄΛࣔ͢ OPCODE=1ͳͷͰςΩετϑϨʔϜͰ͋ΔࣄΛࣔ͢ ϝοηʔδͷ࠷ޙͷςΩετϑϨʔϜͰ͋Δ
©Fusic Co., Ltd. 23 WebSocketϑϨʔϜ 23 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 81: FIN=1, OPCODE=1 (ςΩετϑϨʔϜ) OPCODE (4Ϗοτ)ʹҎԼͷϑϨʔϜ͕͋Γ·͢ 0x1: ςΩετϑϨʔϜ 0x2: όΠφϦϑϨʔϜ 0x8: ଓͷऴྃ 0x9: Ping 0xA: Pong
©Fusic Co., Ltd. 24 WebSocketϑϨʔϜ 24 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 85: MASK=1, ϖΠϩʔυ=5 MASK (1Ϗοτ):σʔλ͕ϚεΫ͞Ε͍ͯΔ͔Λࣔ͢ ΫϥΠΞϯτ͔Βૹ৴͞ΕΔϑϨʔϜ ඞͣϚεΫ͞Ε͍ͯΔ (1) αʔόʔ͔ΒͷϑϨʔϜ௨ৗϚεΫ͞Εͳ͍ (0)
©Fusic Co., Ltd. 25 WebSocketϑϨʔϜ 25 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 85: MASK=1, ϖΠϩʔυ=5 Payload Len (7Ϗοτ) :ϖΠϩʔυͷ͞Λࣔ͢ 0 ~ 125: ࣮ࡍͷϖΠϩʔυ 126: ࣍ͷ2όΠτ͕ϖΠϩʔυΛࣔ͢(16Ϗοτ) 127: ࣍ͷ8όΠτ͕ϖΠϩʔυΛࣔ͢(64Ϗοτ)
©Fusic Co., Ltd. 26 WebSocketϑϨʔϜ 26 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 37 FA 21 3D: ϚεΫΩʔ Masking Key (4όΠτ):ϖΠϩʔυσʔλΛ ϚεΫ͢ΔͨΊͷϥϯμϜͳΩʔ ΫϥΠΞϯτૹ৴࣌ͷΈؚ·ΕΔʢޮԽͷҝʣ αʔόʔଆϚεΫΩʔΛऔಘͯ͠ϚεΫղআ͢Δ தؒϓϩΩγ͕σʔλΛޡղऍ͠ͳ͍Α͏ʹ ϚεΫΛద༻
©Fusic Co., Ltd. 27 WebSocketϑϨʔϜ 27 Client server Response Request
8 1 85 37 FA 2 1 3 D 7 F 9 F 4 D 5 1 5 8 7F 9F 4D 51 58ɿPayload Data (ϚεΫ͞Εͨσʔλ) ϚεΫ͞ΕͨσʔλͰ͢ HelloΛϚεΫΩʔΛར༻ͯ͠XORԋࢉ͞Ε͓ͯΓɺ αʔόʔϧʔϧʹج͍ͮͯϚεΫΛղআͯ͠ɺ ϖΠϩʔυΛར༻͠·͢
©Fusic Co., Ltd. 28 ͍ϖΠϩʔυ (>125όΠτ) ͷ WebSocket ϑϨʔϜ 28
Client server Response Request 8 1 FE 00 C 8 1 2 34 5 6 7 8 125όΠτΑΓ͍ϖΠϩʔυͷ߹ɺ ϔομʔͱϖΠϩʔυΛׂ ҎԼ126όΠτͣͭɺ200όΠτͷσʔλΛૹ৴ 81: FIN=1, OPCODE=1ʢςΩετϑϨʔϜʣ FE: MASK=1, ϖΠϩʔυ=126 00 C8: ֦ுϖΠϩʔυ=200ɹ 12 34 56 78: ϚεΫΩʔ
©Fusic Co., Ltd. 29 PingϑϨʔϜ 29 Client server Response Request
8 9 81 12 34 56 78 89: FIN=1, OPCODE=9ʢPingϑϨʔϜʣ 81: MASK=1, ϖΠϩʔυ=1 12 34 56 78: ϚεΫΩʔ ϖΠϩʔυͳ͠
©Fusic Co., Ltd. 30 σϞ 3
©Fusic Co., Ltd. 31 JavaScriptଆͷ࣮ ଓʹདྷͨϢʔβʔͷϝοηʔδ͕ɺଞͷਓͷϒϥβʹΘΔ http://࣮ࡍͷࢿྉʹ͚ͩషΓ͚͍ͯ·͢
©Fusic Co., Ltd. 32 ιʔείʔυެ։͍ͯ͠·͢ https://github.com/seike460/php-websocket-original ొஃલʹXʹϙετ͍ͯ͠·͢ͷͰɺ ͥͻʮwebsokects.phpʯΛࢀর͠ͳ͕Β ௌߨ͚·͢ͱ͍Ͱ͢ σϞʹ͍͖·͢
©Fusic Co., Ltd. 33 JavaScriptଆͷ࣮ 4
©Fusic Co., Ltd. 34 JavaScriptଆͷ࣮ const WEBSOCKET_URL = "ws://localhost:8080" socket
= new WebSocket(WEBSOCKET_URL) socket.send(payload) -> ଓઌͷSocketॻ͖ࠐΈ(PHPαʔόʔʹॻࠐΈ) socket.onopen = function (event) {} WebSocketͷଓ͕ޭͨ͠ͱ͖ʹݺͼग़͞Ε·͢ socket.onmessage = function (event) {} αʔόʔ͔ΒϝοηʔδΛड৴ͨ͠ͱ͖ʹݺͼग़͞Ε·͢ socket.onerror = function (error) {} WebSocketଓͰΤϥʔ͕ൃੜͨ͠ͱ͖ʹݺͼग़͞Ε·͢ socket.onclose = function (event) {} WebSocketଓ͕ด͡ΒΕͨͱ͖ʹݺͼग़͞Ε·͢
©Fusic Co., Ltd. 35 JavaScriptଆͷ࣮ socket.onopen = function (event) {}
WebSocketͷଓ͕ޭͨ͠ͱ͖ʹݺͼग़͞Ε·͢ eventΦϒδΣΫτͷதʹҎԼ͕ೖ͍ͬͯ·͢
©Fusic Co., Ltd. 36 JavaScriptଆͷ࣮ socket.onmessage = function (event) {}
αʔόʔ͔ΒϝοηʔδΛड৴ͨ͠ͱ͖ʹݺͼग़͞Ε·͢ eventΦϒδΣΫτͷதʹҎԼ͕ೖ͍ͬͯ·͢
©Fusic Co., Ltd. 37 JavaScriptଆͷ࣮ socket.onerror = function (error) {}
WebSocketଓͰΤϥʔ͕ൃੜͨ͠ͱ͖ʹݺͼग़͞Ε·͢ ErrorΦϒδΣΫτͷதʹҎԼ͕ೖ͍ͬͯ·͢
©Fusic Co., Ltd. 38 JavaScriptଆͷ࣮ socket.onclose = function (event) {}
WebSocketଓ͕ด͡ΒΕͨͱ͖ʹݺͼग़͞Ε·͢ eventΦϒδΣΫτͷதʹҎԼ͕ೖ͍ͬͯ·͢
©Fusic Co., Ltd. 39 PHPଆͷ࣮ 5
©Fusic Co., Ltd. 40 αʔόʔͷॳظԽ
©Fusic Co., Ltd. 41 αʔόʔͷॳظԽ socket_create:ιέοτΛ࡞ ɹAF_INET: IPv4Λ༻ ɹSOCK_STREAM: TCPϓϩτίϧΛࢦఆ
ɹSOL_TCP: ιέοτͷλΠϓ
©Fusic Co., Ltd. 42 αʔόʔͷॳظԽ socket_set_option:ιέοτΦϓγϣϯΛઃఆ ɹSO_REUSEADDR: ϙʔτͷ࠶ར༻ΛڐՄ
©Fusic Co., Ltd. 43 αʔόʔͷॳظԽ socket_bind:ࢦఆ͞ΕͨIPΞυϨεͱϙʔτʹιέοτΛόΠϯυ
©Fusic Co., Ltd. 44 αʔόʔͷॳظԽ socket_listen:ιέοτΛϦοεϯঢ়ଶʹ͠ɺΫϥΠΞϯτଓΛͪड͚ ɹͪड͚ΔΫϥΠΞϯτͷ(=$maxClients)5 ʹͯ͠·͢
©Fusic Co., Ltd. 45 ϝΠϯϧʔϓʢଓͪʣ
©Fusic Co., Ltd. 46 ϝΠϯϧʔϓʢଓͪʣ socket_select:ଓιέοτʢಡΈऔΓՄೳͳιέοτʣΛࢹ ɹಡΈऔΓՄೳʢ$readʣ: ιέοτʹड৴σʔλ͕͋ΓɺಡΈऔΕΔ ɹॻ͖ࠐΈՄೳʢ$writeʣ: ιέοτ͕ॻ͖ࠐΈՄೳ
ɹΤϥʔൃੜʢ$exceptʣ: ιέοτʹΤϥʔ͕ൃੜ ɹλΠϜΞτʢnullʣ:λΠϜΞτ͢Δඵɹ˞ࠓճແݶ
©Fusic Co., Ltd. 47 ϝΠϯϧʔϓʢଓͪʣ socket_acceptɿ৽نΫϥΠΞϯτଓͷड͚ೖΕ αʔόʔιέοτʢ$serverʣ͕ಡΈऔΓՄೳͷ߹ɺ ৽͍͠ΫϥΠΞϯτଓΛड͚ೖΕ ৽͍͠ιέοτʢΫϥΠΞϯτʣΛ $clients
ྻʹՃ
©Fusic Co., Ltd. 48 σʔλड৴(σίʔυ)ͱϋϯυγΣΠΫ
©Fusic Co., Ltd. 49 σʔλड৴(σίʔυ)ͱϋϯυγΣΠΫ socket_recvɿΫϥΠΞϯτιέοτ͔ΒσʔλΛड৴ ड৴σʔλ͕ͳ͍߹($bytes === 0) ΫϥΠΞϯτ͕அ͞ΕͨͱΈͳ͠ιέοτΛด͡Δ
©Fusic Co., Ltd. 50 σʔλड৴(σίʔυ)ͱϋϯυγΣΠΫ ▪·ͩHandshakeͯ͠ͳ͍Client͔Βͷ௨৴ -> Handshakeཁٻ WebSocketଓͷॳظஈ֊ͰHTTP͔ΒWebSocketͷϓϩτίϧΓସ͑ ࣗ࡞ͷperformHandshake
ؔʢޙड़ʣͰHandshakeΛ࣮ࢪ ▪HandshakeࡁΈͷClient͔Βͷ௨৴ -> σʔλ͕ૹ৴͞Ε͖͍ͯͯΔ WebSocketͷϑϨʔϜΛdecodeؔʢޙड़ʣΛར༻ͯ͠ཧղͰ͖ΔΑ͏ʹ
©Fusic Co., Ltd. 51 ΫϥΠΞϯτ͔ΒͷσʔλΛશΫϥΠΞϯτૹ৴
©Fusic Co., Ltd. 52 ΫϥΠΞϯτ͔ΒͷσʔλΛશΫϥΠΞϯτૹ৴ ͜͜γϯϓϧʹΫϥΠΞϯτ͔Βड͚औͬͨϖΠϩʔυΛՃͯ͠ɺ ଞͷΫϥΠΞϯτͷSocketʹॻ͖ࠐΈ·͢ ͦ͏͢Δ͜ͱͰɺผͷϒϥβʹॻ͖ࠐ·Εͨσʔλ͕දࣔ͞Ε·͢
©Fusic Co., Ltd. 53 ϋϯυγΣΠΫؔ
©Fusic Co., Ltd. 54 ϋϯυγΣΠΫؔ ·ͣSec-WebSocket-Key͕ඞͣ͋ΔͣͳͷͰऔಘ ͳ͍߹ෆਖ਼ͳ௨৴ͳͷͰSocketΛด͡Δ
©Fusic Co., Ltd. 55 ϋϯυγΣΠΫؔ ͦͷޙऔಘͨ͠Sec-WebSocket-KeyͱGUIDΛ݁߹ͯ͠SHA-1ͰϋογϡԽ
©Fusic Co., Ltd. 56 ϋϯυγΣΠΫؔ ϋογϡԽͨ͠ΩʔͱɺඞཁͳϔομʔΛ༩ͯ͠௨৴͖ͯͨ͠ ΫϥΠΞϯτʹResponse͢Δҝʹɺsocket_writeΛߦ͏
©Fusic Co., Ltd. 57 ड৴σʔλͷσίʔυ
©Fusic Co., Ltd. 58 ड৴σʔλͷσίʔυ ord($data[1]) & 127 ͰجຊϖΠϩʔυΛऔಘ 126
·ͨ 127 ͷ߹ʹ֦ுϖΠϩʔυΛಡΈऔΔ ֦ுϖΠϩʔυͷޙʹ͋Δ ϚεΫΩʔʢ4όΠτʣ ΛಡΈऔΓɺ ϖΠϩʔυσʔλ։࢝ҐஔΛܭࢉ
©Fusic Co., Ltd. 59 ड৴σʔλͷσίʔυ ϑϨʔϜͷϔομʔ෦ΛεΩοϓ͠ɺ $payloadOffset ͷҐஔ͔ΒϖΠϩʔυσʔλΛऔಘ for จͰϖΠϩʔυͷ֤όΠτΛϚεΫΩʔͱ
XORԋࢉ $unmaskedPayload ʹ෮߸σʔλΛߏங
©Fusic Co., Ltd. 60 WebSocketࢥͬͨΑΓγϯϓϧͳ௨৴ WebSocketͱ͍͏௨৴ʮϦΞϧλΠϜ௨৴ʯͱݴΘΕΔ ෳࡶͩͱײ͍ͯ͡Δํ͍Βͬ͠ΌΔ͔Ͱ͕͢ɺ ࢥ͍ͷ֎ɺHTTP௨৴ͷཁૉ͕େ͖͔ͬͨͰ͢ WebSocketΛۙʹɺϦΞΫςΟϒͳPHPΛߏங͠·͠ΐ͏
©Fusic Co., Ltd. 61 ͱ͍͑… ੈͷதʹศརͳOSS͕͋Γ·͢ͷͰɺجຊతʹͦͪΒΛར༻͠·͠ΐ͏ PHP͕͑ΔͷͳΒ https://github.com/walkor/workerman ͳͲ Amazon
API Gateway WebSocketରԠͯ͠ΔͷͰ AWS Lambda + BrefͳσϞ࡞ࡁΈͰ͢ͷͰ߹Θͤͯࢼͯ͠ݟ͍ͯͩ͘͞
©Fusic Co., Ltd. 62 Ϋϥυʹ·͔ͤͪΌ͏ͷख Amazon API Gateway WebSocketରԠͯ͠ΔͷͰ AWS
Lambda + BrefͳσϞ͕࡞ग़དྷΔϦϙδτϦ͋Γ·͢ͷͰ ࢼͯ͠Έ͍ͯͩ͘͞ https://github.com/seike460/php-websocket-bref
©Fusic Co., Ltd. 63 ·ͱΊ 5
©Fusic Co., Ltd. 64 ·ͱΊ WebSocketํʹϦΞϧλΠϜ௨৴Ͱ͖ΔɺϦΞΫςΟϒͳΞϓϦʔέʔγϣϯʹదͨ͠ϓϩτίϧ༷ Point 01 WebSocketͷ௨৴ࣗମཧղ͢ΕHTTPͷԆͱͯ͠ѻ͑Δɻंྠͷ࠶ൃ໌ʹͯ௨৴༷Λཧղ Point
02 ࣮ࡍ https://github.com/walkor/workerman ͳͲ͕Φεεϝ Point 03 Ϋϥυʹ·͔ͤͯ͠·͏ͷҰͭͷखஈɺ༷ʑͳબࢶΛ࣋ͪ·͠ΐ͏ Point 04
©Fusic Co., Ltd. 65 Thank You We are Hiring! https://recruit.fusic.co.jp/
͝ਗ਼ௌ͍͖ͨͩ͋Γ͕ͱ͏͍͟͝·ͨ͠