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
650
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
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
160
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
530
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
1.1k
実践サーバーレスパフォーマンスチューニング ~その実力に迫る~ / Practical Serverless Performance Tuning ~A Close Look at its Power~
seike460
PRO
2
350
PHPを書く理由、PHPを書いていて良い理由 / Reasons to write PHP and why it is good to write PHP
seike460
PRO
5
600
AWS CDKを用いたセキュアなCI/CDパイプラインの構築 / Build a secure CI/CD pipeline using AWS CDK
seike460
PRO
3
760
いまあるチームにフィットさせる Serverless そして Platform Engineeringへの挑戦 / Serverless Fits the Team You Have and Platform Engineering
seike460
PRO
2
2.1k
いまあるチームにフィットさせる Serverless / Serverless fits in with the team you have now.
seike460
PRO
2
190
地方こそサーバーレス! チームにフィットさせるサーバーレス / Rural areas are serverless! Serverless to Fit Your Team
seike460
PRO
1
120
Other Decks in Programming
See All in Programming
Zoneless Testing
rainerhahnekamp
0
120
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
810
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
140
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
350
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
440
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
7
1.5k
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
940
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
510
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
380
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
33
1.5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Visualization
eitanlees
146
15k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Building Your Own Lightsaber
phodgson
103
6.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Building Applications with DynamoDB
mza
91
6.1k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
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/
͝ਗ਼ௌ͍͖ͨͩ͋Γ͕ͱ͏͍͟͝·ͨ͠