[ã¢ããã€ã¹ãèªã]ãµã€ã¯ã«ã®ä»ã®19ã®éšå
ããŒã1ïŒ ãšã³ãžã³ãã©ã³ã¿ã€ã ã¡ã«ããºã ãã³ãŒã«ã¹ã¿ãã¯ã®æŠèŠ
ããŒã2ïŒ V8å éšãšã³ãŒãã®æé©åã«ã€ããŠ
ããŒã3ïŒ ã¡ã¢ãªç®¡çã4çš®é¡ã®ã¡ã¢ãªãªãŒã¯ãããã³ããããšã®æŠã
ããŒã4ïŒ ã€ãã³ãã«ãŒããéåæãããã³éåæ/åŸ æ©ã䜿çšããŠã³ãŒããæ¹åãã5ã€ã®æ¹æ³
ããŒã5ïŒ WebSocketãšHTTP / 2 + SSEã äœãéžã¶ïŒ
ããŒã6ïŒ WebAssemblyã®æ©èœãšç¯å²
ããŒã7ïŒ Web Workersãš5ã€ã®äœ¿çšã·ããªãª
ããŒã8ïŒ ãµãŒãã¹ã¯ãŒã«ãŒ
ããŒã9ïŒ Webããã·ã¥éç¥
ããŒã10ïŒ MutationObserverã䜿çšããŠDOMã®å€æŽã远跡ãã
ããŒã11ïŒ WebããŒãžã¬ã³ããªã³ã°ãšã³ãžã³ãšããã©ãŒãã³ã¹ãæé©åããããã®ãã³ã
ããŒã12ïŒ ããã©ãŒãã³ã¹ãšã»ãã¥ãªãã£ãæé©åãããã©ãŠã¶ãŒã®ãããã¯ãŒã¯ãµãã·ã¹ãã
ããŒã12ïŒ ããã©ãŒãã³ã¹ãšã»ãã¥ãªãã£ãæé©åãããã©ãŠã¶ãŒã®ãããã¯ãŒã¯ãµãã·ã¹ãã
ããŒã13ïŒ CSSãšJavaScriptã䜿çšããã¢ãã¡ãŒã·ã§ã³
ããŒã14ïŒ JSã®ä»çµã¿ïŒæœè±¡æ§æããªãŒã解æãããã³ãã®æé©å
ããŒã15ïŒ JSã®ä»çµã¿ïŒã¯ã©ã¹ãšç¶æ¿ãBabelããã³TypeScriptã§ã®ãã©ã³ã¹ãã¬ãŒã·ã§ã³
ããŒã16ïŒ JSã®ä»çµã¿ïŒã¹ãã¬ãŒãž
ããŒã17ïŒ JSã®ä»çµã¿ïŒShadow DOMãã¯ãããžãŒãšWebã³ã³ããŒãã³ã
ããŒã18ïŒ JSã®ä»çµã¿ïŒWebRTCããã³P2Péä¿¡ã¡ã«ããºã
ããŒã19ïŒ JSã®ä»çµã¿ïŒã«ã¹ã¿ã èŠçŽ
ããŒã2ïŒ V8å éšãšã³ãŒãã®æé©åã«ã€ããŠ
ããŒã3ïŒ ã¡ã¢ãªç®¡çã4çš®é¡ã®ã¡ã¢ãªãªãŒã¯ãããã³ããããšã®æŠã
ããŒã4ïŒ ã€ãã³ãã«ãŒããéåæãããã³éåæ/åŸ æ©ã䜿çšããŠã³ãŒããæ¹åãã5ã€ã®æ¹æ³
ããŒã5ïŒ WebSocketãšHTTP / 2 + SSEã äœãéžã¶ïŒ
ããŒã6ïŒ WebAssemblyã®æ©èœãšç¯å²
ããŒã7ïŒ Web Workersãš5ã€ã®äœ¿çšã·ããªãª
ããŒã8ïŒ ãµãŒãã¹ã¯ãŒã«ãŒ
ããŒã9ïŒ Webããã·ã¥éç¥
ããŒã10ïŒ MutationObserverã䜿çšããŠDOMã®å€æŽã远跡ãã
ããŒã11ïŒ WebããŒãžã¬ã³ããªã³ã°ãšã³ãžã³ãšããã©ãŒãã³ã¹ãæé©åããããã®ãã³ã
ããŒã12ïŒ ããã©ãŒãã³ã¹ãšã»ãã¥ãªãã£ãæé©åãããã©ãŠã¶ãŒã®ãããã¯ãŒã¯ãµãã·ã¹ãã
ããŒã12ïŒ ããã©ãŒãã³ã¹ãšã»ãã¥ãªãã£ãæé©åãããã©ãŠã¶ãŒã®ãããã¯ãŒã¯ãµãã·ã¹ãã
ããŒã13ïŒ CSSãšJavaScriptã䜿çšããã¢ãã¡ãŒã·ã§ã³
ããŒã14ïŒ JSã®ä»çµã¿ïŒæœè±¡æ§æããªãŒã解æãããã³ãã®æé©å
ããŒã15ïŒ JSã®ä»çµã¿ïŒã¯ã©ã¹ãšç¶æ¿ãBabelããã³TypeScriptã§ã®ãã©ã³ã¹ãã¬ãŒã·ã§ã³
ããŒã16ïŒ JSã®ä»çµã¿ïŒã¹ãã¬ãŒãž
ããŒã17ïŒ JSã®ä»çµã¿ïŒShadow DOMãã¯ãããžãŒãšWebã³ã³ããŒãã³ã
ããŒã18ïŒ JSã®ä»çµã¿ïŒWebRTCããã³P2Péä¿¡ã¡ã«ããºã
ããŒã19ïŒ JSã®ä»çµã¿ïŒã«ã¹ã¿ã èŠçŽ
ããã¯ãJSéçºã®æ©èœã«ç¹åããã·ãªãŒãºã®5çªç®ã®è³æã®ç¿»èš³ã§ãã 以åã®èšäºã§ã¯ãJavaScriptãšã³ã·ã¹ãã ã®åºæ¬çãªèŠçŽ ãæ€èšããŸãããJavaScriptãšã³ã·ã¹ãã ã®æ©èœã¯ããµãŒããŒããã³ã¯ã©ã€ã¢ã³ãã³ãŒãã®éçºè ã«ãã£ãŠäœ¿çšãããŠããŸãã ãããã®è³æã§ã¯ãJSã®ç¹å®ã®åŽé¢ã®åºæ¬ã説æããåŸããã®äœ¿çšã«é¢ããæšå¥šäºé ã瀺ããŠããŸãã ãã®èšäºã®èè ã¯ããããã®ååã¯SessionStackã¢ããªã±ãŒã·ã§ã³ã®éçºäžã«é©çšããããšè¿°ã¹ãŠããŸãã ã©ã€ãã©ãªãšãã¬ãŒã ã¯ãŒã¯ã®çŸä»£ã®ãŠãŒã¶ãŒã¯å€ãã®å¯èœæ§ããéžæã§ããã®ã§ãã©ã®ãããžã§ã¯ãã§ãã競äºã«ãµããããèŠããããã«ããããæ§ç¯ãããŠããæè¡ããå¯èœãªãã¹ãŠãçµãåºããªããã°ãªããŸããã
ä»åã¯ãéä¿¡ãããã³ã«ã«ã€ããŠè©±ãããããã®æ©èœãšã³ã³ããŒãã³ããæ¯èŒããŠè°è«ããŸãã ããã§ã¯ãç¹ã«WebSocketãšHTTP / 2ãã¯ãããžãŒãåãäžããã»ãã¥ãªãã£ã«ã€ããŠè©±ããããŸããŸãªç¶æ³ã§é©åãªãããã³ã«ãéžæããããã®ãã³ããå ±æããŸãã
ã¯ããã«
çŸåšãè±å¯ãªåçãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãåããè€éãªWebã¢ããªã±ãŒã·ã§ã³ã¯åœããåã®ããšãšèŠãªãããŠããŸãã ããããã€ã³ã¿ãŒãããã¯çŸåšã®ç¶æ ã«å°éããããã«é·ãéã®ããæ©ããªããã°ãªããŸããã§ããã
åœåãã€ã³ã¿ãŒãããã¯ãã®ãããªã¢ããªã±ãŒã·ã§ã³ããµããŒãããããã«èšèšãããŠããŸããã§ããã çžäºã«ãªã³ã¯ãããããã¥ã¡ã³ãã®ããŠã§ãããšããŠãHTMLããŒãžã®ã³ã¬ã¯ã·ã§ã³ãšããŠèæ¡ãããŸããã åºæ¬çã«ã¯ãã¹ãŠãHTTPãªã¯ãšã¹ã/ã¬ã¹ãã³ã¹ãã©ãã€ã ãäžå¿ã«æ§ç¯ãããŸããã ã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ãããŒãžãããŒãããåŸããŠãŒã¶ãŒããªã³ã¯ãã¯ãªãã¯ããŠæ¬¡ã®ããŒãžã«ç§»åãããŸã§äœãèµ·ãããŸããã§ããã
2005幎é ãAJAXãã¯ãããžãŒãç»å Žããå€ãã®ããã°ã©ããŒãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®åæ¹åéä¿¡ã®å¯èœæ§ãæ¢ãå§ããŸããã ãã ãããã¹ãŠã®HTTPéä¿¡ã»ãã·ã§ã³ã¯åŒãç¶ãã¯ã©ã€ã¢ã³ãã«ãã£ãŠéå§ãããæ°ããããŒã¿ãããŠã³ããŒãããã«ã¯ããŠãŒã¶ãŒã®åå ãŸãã¯ãµãŒããŒãžã®å®æçãªåŒã³åºããå¿ èŠã§ããã
åæ¹åHTTPéä¿¡
ãµãŒããŒããã¯ã©ã€ã¢ã³ãã«ããã¢ã¯ãã£ãã«ããŒã¿ãéä¿¡ã§ããæè¡ã¯ãããªãåããååšããŠããŸããã ãã®äžã«ã¯PushãšCometããããŸãã
ãµãŒããŒãç¬èªã«ããŒã¿ãã¯ã©ã€ã¢ã³ãã«éä¿¡ãããšããé¯èŠãäœæããããã«æãäžè¬çã«äœ¿çšãããææ³ã®1ã€ã¯ãããã³ã°ããŒãªã³ã°ããšåŒã°ããŸãã ãã®ãã¯ãããžãŒã䜿çšããŠãã¯ã©ã€ã¢ã³ãã¯ãµãŒããŒãžã®HTTPæ¥ç¶ãéããå¿çãéä¿¡ããããŸã§ãµãŒããŒãéãããŸãŸã«ããŸãã ãã®çµæããµãŒããŒã«ã¯ã©ã€ã¢ã³ãã®ããŒã¿ãããå ŽåããµãŒããŒã«éä¿¡ãããŸãã
é·ãããŒãªã³ã°ãã¯ãããžãŒãå®è£ ããéåžžã«åçŽãªã³ãŒãã®äŸã次ã«ç€ºããŸãã
(function poll(){ setTimeout(function(){ $.ajax({ url: 'https://api.example.com/endpoint', success: function(data) { // - `data` // ... // poll(); }, dataType: 'json' }); }, 10000); })();
ãã®æ§é ã¯ãåããŠèªåçã«èµ·åãããåŸã«èªåèªèº«ãåŒã³åºãé¢æ°ã§ãã ãµãŒããŒãžã®éåæAjaxåŒã³åºãããšã«10ç§ã®ééãèšå®ãããµãŒããŒã®å¿çãåŠçããåŸãé¢æ°åŒã³åºãã®ã¹ã±ãžã¥ãŒãªã³ã°ãå床å®è¡ããŸãã
ãã®ç¶æ³ã§äœ¿çšãããå¥ã®ææ³ã¯ã FlashãŸãã¯HXRè€åãªã¯ãšã¹ããšãããããhtmlfilesã§ãã
ããããã¹ãŠã®ãã¯ãããžãŒã«ã¯ã1ã€ã®åãåé¡ããããŸããHTTPã®äœ¿çšã«ããäœæãããã·ã¹ãã ãžã®è¿œå ã®è² è·ãããã¯ãé«ãå¿çé床ãå¿ èŠãªã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ãæŽçããã®ã«äžé©åã§ãã ããšãã°ãããã¯ãã«ããã¬ã€ã€ãŒãã©ãŠã¶ã®ãã·ã¥ãŒãã£ã³ã°ã²ãŒã ãããã¢ã¯ã·ã§ã³ããªã¢ã«ã¿ã€ã ã§å®è¡ãããä»ã®ãªã³ã©ã€ã³ã²ãŒã ã®ãããªãã®ã§ãã
WebSocketãã¯ãããžãŒã®æŠèŠ
WebSocketä»æ§ã¯ãWebãã©ãŠã¶ãšãµãŒããŒéã®ãœã±ããããŒã¹ã®æ¥ç¶ã確ç«ããããã®APIãå®çŸ©ããŠããŸãã ç°¡åã«èšãã°ãããã¯ã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®æ°žç¶çãªæ¥ç¶ã§ãããããã䜿çšããŠã¯ã©ã€ã¢ã³ããšãµãŒããŒã¯ãã€ã§ãçžäºã«ããŒã¿ãéä¿¡ã§ããŸãã
ã¯ã©ã€ã¢ã³ãã¯ãããããWebSocketãã³ãã·ã§ã€ã¯ãå®è¡ããŠæ¥ç¶ã確ç«ããŸãã ãã®ããã»ã¹ã¯ãã¯ã©ã€ã¢ã³ããéåžžã®HTTPèŠæ±ããµãŒããŒã«éä¿¡ããããšããå§ãŸããŸãã ãã®èŠæ±ã«ã¯ãã¯ã©ã€ã¢ã³ããWebSocketæ¥ç¶ã確ç«ããããšããµãŒããŒã«æ瀺ãã
Upgrade
ããããŒãå«ãŸããŸãã
ãã®ãããªæ¥ç¶ã®ã€ã³ã¹ããŒã«ãã¯ã©ã€ã¢ã³ãåŽããã©ã®ããã«èŠãããèŠãŠã¿ãŸãããã
// WebSocket-. var socket = new WebSocket('ws://websocket.example.com');
WebSocketæ¥ç¶ã«äœ¿çšãããURLã¯ã
ws
ã¹ããŒã ã䜿çšããŸãã ããã«ãå®å šãªWebSocketæ¥ç¶ãç·šæããããã®
wss
ã¹ããŒã ããããŸããããã¯HTTPSãšåçã§ãã
ãã®å Žåã
websocket.example.com
ãµãŒããŒãšã®WebSocketæ¥ç¶ãéãããã»ã¹ã®éå§ã
websocket.example.com
ãŸãã
å ã®ãªã¯ãšã¹ãã®ããããŒã®ç°¡åãªäŸã次ã«ç€ºããŸãã
GET ws://websocket.example.com/ HTTP/1.1 Origin: http://example.com Connection: Upgrade Host: websocket.example.com Upgrade: websocket
ãµãŒããŒãWebSocketãããã³ã«ããµããŒãããŠããå Žåã¯ããµãŒããŒã«åãæ¿ããŠ
Upgrade
å¿çããããŒã§å ±åããããšã«åæããŸãã Node.jsã䜿çšãããã®ã¡ã«ããºã ã®å®è£ ãèŠãŠã¿ãŸãããã
// WebSocket //https://github.com/theturtle32/WebSocket-Node var WebSocketServer = require('websocket').server; var http = require('http'); var server = http.createServer(function(request, response) { // HTTP-. }); server.listen(1337, function() { }); // wsServer = new WebSocketServer({ httpServer: server }); // WebSocket- wsServer.on('request', function(request) { var connection = request.accept(null, request.origin); // - , // . connection.on('message', function(message) { // WebSocket }); connection.on('close', function(connection) { // }); });
æ¥ç¶ã確ç«ããããšããµãŒããŒã®å¿çã«ã¯WebSocketãããã³ã«ãžã®ç§»è¡ã«é¢ããæ å ±ãå«ãŸããŸãã
HTTP/1.1 101 Switching Protocols Date: Wed, 25 Oct 2017 10:07:34 GMT Connection: Upgrade Upgrade: WebSocket
ãã®åŸãã¯ã©ã€ã¢ã³ãã®WebSocketã€ã³ã¹ã¿ã³ã¹ã§
open
ã€ãã³ããçºçããŸãã
var socket = new WebSocket('ws://websocket.example.com'); // WebSocket-. socket.onopen = function(event) { console.log('WebSocket is connected.'); };
ãã³ãã·ã§ã€ã¯ãã§ãŒãºã®å®äºåŸãå ã®HTTPæ¥ç¶ã¯ãåãåºæ¬çãªTCP / IPæ¥ç¶ã䜿çšããWebSocketæ¥ç¶ã«çœ®ãæããããŸãã ãã®æç¹ã§ãã¯ã©ã€ã¢ã³ããšãµãŒããŒã®äž¡æ¹ãããŒã¿ã®éä¿¡ãéå§ã§ããŸãã
WebSocketã䜿çšããããšã«ãããåŸæ¥ã®HTTPãªã¯ãšã¹ãã®äœ¿çšã«ããäžå¿ èŠãªè² è·ã«ã·ã¹ãã ããããããšãªããä»»æã®éã®ããŒã¿ãéä¿¡ã§ããŸãã ããŒã¿ã¯ã¡ãã»ãŒãžã®åœ¢åŒã§WebSocketæ¥ç¶ãä»ããŠéä¿¡ãããåã¡ãã»ãŒãžã¯éä¿¡ãããããŒã¿ãå«ã1ã€ä»¥äžã®ãã¬ãŒã ã§æ§æãããŸãïŒãã€ããŒãïŒã ã¯ã©ã€ã¢ã³ãã«å°éãããšãã«å ã®ã¡ãã»ãŒãžã®æ£ããã¢ã»ã³ããªã確ä¿ããããã«ãåãã¬ãŒã ã«ã¯ããã€ããŒãã«é¢ãã4ã12ãã€ãã®ããŒã¿ãå«ããã¬ãã£ãã¯ã¹ããããŸãã ãã¬ãŒã ããŒã¹ã®ã¡ãã»ãŒãžã³ã°ã·ã¹ãã ã䜿çšãããšãéä¿¡ãã£ãã«ãä»ããŠéä¿¡ããããµãŒãã¹ããŒã¿ã®æ°ãæžããããšãã§ããæ å ±éä¿¡ã®é 延ãå€§å¹ ã«åæžãããŸãã
ãã¹ãŠã®ãã¬ãŒã ãåä¿¡ãããå ã®ã¡ãã»ãŒãžãã€ããŒããåæ§ç¯ãããåŸã«ã®ã¿ãã¯ã©ã€ã¢ã³ãã«æ°ããã¡ãã»ãŒãžãéç¥ãããããšã«æ³šæããŠãã ããã
ããŸããŸãªWebSocketãããã³ã«URL
åè¿°ã®ããã«ãWebSocketã¯æ°ããURLã¹ããŒã ã䜿çšããŸãã å®éããããã®2ã€ããããŸãïŒ
ws://
ããã³
wss://
ã
URLãäœæãããšããç¹å®ã®ã«ãŒã«ã䜿çšãããŸãã WebSocket URLã®æ©èœã¯ãã¢ã³ã«ãŒïŒ
#sample_anchor
ïŒããµããŒãããªãããšã§ãã
ãã以å€ã®å ŽåãWebSocket URLã«ã¯HTTP URLãšåãã«ãŒã«ãé©çšãããŸãã wsã¢ãã¬ã¹ã䜿çšããå Žåãæ¥ç¶ã¯æå·åããããããã©ã«ãã§ã¯ããŒã80ã䜿çšãããŸãwssã䜿çšããå ŽåãTLSæå·åãå¿ èŠã§ãããããŒã443ã䜿çšãããŸãã
ãã¬ãŒã ãããã³ã«
WebSocketãã¬ãŒã ãæäœããããã®ãããã³ã«ã詳ããèŠãŠãã ããã 以äžã¯ã察å¿ããRFCãããã¬ãŒã æ§é ã«ã€ããŠåŠã¶ããšãã§ãããã®ã§ãã
æšæºåãããRFCã§ããWebSocketã®ããŒãžã§ã³ã«ã€ããŠè©±ããšãåããã±ãŒãžã®å é ã«å°ããªããããŒããããšèšããŸãã ããããããã¯éåžžã«å°é£ã§ãã ã³ã³ããŒãã³ãã®èª¬æã¯æ¬¡ã®ãšããã§ãã
-
fin
ïŒ1ãããïŒïŒãã®ãã¬ãŒã ãã¡ãã»ãŒãžã®éä¿¡ãå®äºããæåŸã®ãã¬ãŒã ã§ãããã©ããã瀺ããŸãã ã»ãšãã©ã®å Žåãã¡ãã»ãŒãžãéä¿¡ããã«ã¯1ã€ã®ãã¬ãŒã ã§ååã§ããããã®ãããã¯åžžã«èšå®ãããŠããŸãã å®éšã«ãããFirefoxã¯ã¡ãã»ãŒãžãµã€ãºã32 Kbãè¶ ãããš2çªç®ã®ãã¬ãŒã ãäœæããããšã瀺ããŠããŸãã
-
rsv1
ãrsv2
ãrsv3
ïŒãããã1ãããç®ïŒïŒãããã®ãã£ãŒã«ãã¯ãæ¡åŒµã«åæããªãå Žåã«ã®ã¿0ã«èšå®ããå¿ èŠããããŸããããã«ãããéãŒãå€ã®æå³ã決ãŸããŸãã ãããã®ãã£ãŒã«ãã®ããããã«ãŒã以å€ã®å€ãèšå®ããããã®å€ã®æå³ã«ã€ããŠåæã«éããŠããªãå Žåãåä¿¡è ã¯æ¥ç¶ãç¡å¹ã«ããå¿ èŠããããŸãã
-
opcode
ïŒ4ãããïŒïŒãã¬ãŒã ã®ã³ã³ãã³ãã¯ããã§ãšã³ã³ãŒããããŸãã çŸåšã次ã®å€ã䜿çšãããŸãã
-
0x00
ïŒãã®ãã¬ãŒã ã«ã¯ãéä¿¡ãããã¡ãã»ãŒãžã®æ¬¡ã®éšåãå«ãŸããŸãã
-
0x01
ïŒãã®ãã¬ãŒã ã«ã¯ããã¹ãããŒã¿ãå«ãŸããŸãã
-
0x02
ïŒãã€ããªããŒã¿ã¯ãã®ãã¬ãŒã ã«ãããŸãã
-
0x08
ïŒãã®ãã¬ãŒã ã¯æ¥ç¶ãçµäºããŸãã
-
0x09
ïŒããã¯pingãã¬ãŒã ã§ãã
-
0x0a
ïŒããã¯ãã³ãã³ãã¬ãŒã ã§ãã
-
ã芧ã®ãšãããããã«ã¯ååãªæªäœ¿çšã®å€ããããŸãã ãããã¯å°æ¥ã®ããã«äºçŽãããŠããŸãã
-
mask
ïŒ1ãããïŒïŒãã¬ãŒã ããã¹ã¯ãããŠããããšã瀺ããŸãã çŸåšã®ç¶æ³ã§ã¯ãã¯ã©ã€ã¢ã³ããããµãŒããŒãžã®ãã¹ãŠã®ã¡ãã»ãŒãžããã¹ã¯ããå¿ èŠããããŸããããããªããšãä»æ§ã§åæãå¿ èŠã«ãªããŸãã
-
payload_len
ïŒ7ãããïŒïŒãã€ããŒãã®é·ãã WebSocketãã¬ãŒã ã¯ããã€ããŒããµã€ãºãæå®ããããã®ä»¥äžã®ã¡ãœããããµããŒãããŠããŸãã å€0ã125ã¯ããã€ããŒãã®é·ãã瀺ããŸãã 126ã¯ã次ã®2ãã€ãã®å¹³åãµã€ãºãæå³ããŸãã 127ã¯ã次ã®8ãã€ãã«ãµã€ãºæ å ±ãå«ãŸããããšãæå³ããŸãã ãã®çµæããã€ããŒãã®é·ãã¯çŽ7ãããã16ããŸãã¯64ã§èšè¿°ã§ããŸãã
-
masking-key
ïŒ32ãããïŒïŒã¯ã©ã€ã¢ã³ããããµãŒããŒã«éä¿¡ããããã¹ãŠã®ãã¬ãŒã ã¯ããã¬ãŒã ã«å«ãŸãã32ãããå€ã䜿çšããŠãã¹ã¯ãããŸãã
-
payload
ïŒããããåœè£ ããããã¬ãŒã ã§éä¿¡ãããããŒã¿ã ãããã®é·ãã¯ãpayload_len
èšå®ã«å¯Ÿå¿ããŠããŸãã
WebSocketãããã³ã«ãã¹ã¬ããã§ã¯ãªããã¬ãŒã ã«åºã¥ããŠããã®ã¯ãªãã§ããïŒ ãã®è³ªåã«å¯Ÿããçããããã£ãŠããå Žåã¯ãã³ã¡ã³ãã§å ±æã§ããŸãã ãŸããHackerNewsã®ãã®ãããã¯ã«é¢ããèå³æ·±ãè°è«ããããŸãã
ãã¬ãŒã å ã®ããŒã¿
ãã§ã«è¿°ã¹ãããã«ãããŒã¿ã¯å€ãã®ãã¬ãŒã ã«åå²ã§ããŸãã ããŒã¿è»¢éãéå§ãããæåã®ãã¬ãŒã ã®
opcode
ãã£ãŒã«ãã«ã¯ãéä¿¡ãããããŒã¿ã®ã¿ã€ããèšå®ãããŸãã JavaScriptã§ã¯ãWebSocketä»æ§ã®äœæ¥éå§æã«ãã€ããªããŒã¿ã®ãµããŒãããªãã£ããšèšãããšãã§ããããããããå¿ èŠã§ãã ã³ãŒã
0x01
ã¯UTF-8ãšã³ã³ãŒãããŒã¿ã瀺ããã³ãŒã
0x02
ãã€ããªããŒã¿ã«äœ¿çšãããŸãã å€ãã®å ŽåãWebSocketãã±ããã§ã¯ãJSONããŒã¿ãéä¿¡ãããŸããéåžžãJSONããŒã¿ã¯ããã¹ãã®ããã«
opcode
ãã£ãŒã«ããèšå®ããŸãã ãã€ããªããŒã¿ã転éããå Žåããããã¯Webãã©ãŠã¶åºæã®Blobãšã³ãã£ãã£ã®åœ¢åŒã§è¡šç€ºãããŸãã
WebSocketãããã³ã«ãä»ããŠããŒã¿ãéä¿¡ããããã®APIã¯éåžžã«ç°¡åã§ãã
var socket = new WebSocket('ws://websocket.example.com'); socket.onopen = function(event) { socket.send('Some message'); // . };
ã¯ã©ã€ã¢ã³ãåŽã§WebSocketãããŒã¿ãåä¿¡ãããšã
message
ã€ãã³ããçºç
message
ãŸãã ãã®ã€ãã³ãã«ã¯ãã¡ãã»ãŒãžã³ã³ãã³ãã®æäœã«äœ¿çšã§ãã
data
ããããã£ããããŸãã
// , . socket.onmessage = function(event) { var message = event.data; console.log(message); };
Chromeéçºè ããŒã«ã®[ãããã¯ãŒã¯]ã¿ãã䜿çšããŠãWebSocketæ¥ç¶ã®ãã¬ãŒã å ã®å 容ã確èªã§ããŸãã
ããŒã¿ã®æçå
æçšãªããŒã¿ã¯ãããã€ãã®å¥ã ã®ãã¬ãŒã ã«åå²ã§ããŸãã åä¿¡åŽã¯ã
fin
ããããŒãã£ãŒã«ããèšå®ããããã¬ãŒã ãå°çãããŸã§ããã¬ãŒã ããããã¡ãªã³ã°ãããšæ³å®ãããŠããŸãã ãã®çµæãããšãã°ãHello Worldã¡ãã»ãŒãžã¯11ãã¬ãŒã ã§éä¿¡ã§ããåãã¬ãŒã ã¯1ãã€ãã®ãã€ããŒããš6ãã€ãã®ããããŒããŒã¿ãäŒéããŸãã å¶åŸ¡ãã±ããã®æçåã¯çŠæ¢ãããŠããŸãã ãã ãããã®ä»æ§ã§ã¯ã 亀äºã®å¶åŸ¡ãã¬ãŒã ãåŠçã§ããŸãã ããã¯ãTCPãã±ãããã©ã³ãã ãªé åºã§å°çããå Žåã«å¿ èŠã§ãã
äžè¬çã«ããã¬ãŒã ãçµåããããžãã¯ã¯æ¬¡ã®ããã«ãªããŸãã
- æåã®ãã¬ãŒã ãåãå
¥ããŸãã
-
opcode
ãã£ãŒã«ãã®å€ãèŠããŠãããŠãã ããã
- ä»ã®ãã¬ãŒã ãåãå
¥ãã
fin
ããããèšå®ããããã¬ãŒã ãåä¿¡ããããŸã§ããã¬ãŒã ã®ãã€ããŒããçµåããŸãã
- æåã®ãã¬ãŒã ãé€ããã¹ãŠã®ãã¬ãŒã ã®
opcode
ãã£ãŒã«ãããŒãã«èšå®ãããŠããããšã確èªããŸãã
æçåã®äž»ãªç®çã¯ãããŒã¿ãéä¿¡ãããæç¹ã§ãµã€ãºãäžæãªã¡ãã»ãŒãžã®éä¿¡ãèš±å¯ããããšã§ãã
æçåã®ãããã§ããµãŒããŒã¯é©åãªãµã€ãºã®ãããã¡ãŒãéžæãããããã¡ãŒããã£ã±ãã«ãªã£ãããããã¯ãŒã¯ã«ããŒã¿ãéä¿¡ã§ããŸãã æçåã®2çªç®ã®äœ¿çšäŸã¯ãã¡ãã»ãŒãžãè«çéä¿¡ãã£ãã«å šäœãå æããããšãæãŸãããªãå Žåã®å€éåã§ãã ãã®çµæãå€éåã®ç®çã§ããã£ãã«å ±æãããé©åã«ç·šæããããã«ãã¡ãã»ãŒãžãããå°ããªãã©ã°ã¡ã³ãã«åå²ã§ããå¿ èŠããããŸãã
ããŒãããŒãã¡ãã»ãŒãžã«ã€ããŠ
ãã³ãã·ã§ã€ã¯åŸã¯ãã€ã§ããã¯ã©ã€ã¢ã³ããŸãã¯ãµãŒããŒã®ããããããçžæåŽã«pingã¡ãã»ãŒãžãéä¿¡ããããšã決å®ã§ããŸãã ãã®ãããªã¡ãã»ãŒãžãåä¿¡ãããšãåä¿¡è ã¯ã§ããã ãæ©ããã³ã¡ãã»ãŒãžãéä¿¡ããå¿ èŠããããŸãã ãããã¯ããŒãããŒãã¡ãã»ãŒãžã§ãã ã¯ã©ã€ã¢ã³ãããŸã ãµãŒããŒã«æ¥ç¶ãããŠãããã©ããã確èªããããã«äœ¿çšã§ããŸãã
pingããã³pongã¡ãã»ãŒãžã¯åãªãå¶åŸ¡ãã¬ãŒã ã§ãã pingã¡ãã»ãŒãžã®å Žåã
opcode
ãã£ãŒã«ãã¯
0x9
ã«èšå®ãããpongã¡ãã»ãŒãžã®å Žåã¯
0xA
èšå®ãããŸãã pingã¡ãã»ãŒãžãåä¿¡ãããšãpingã¡ãã»ãŒãžãšåããã€ããŒããå«ãpongã¡ãã»ãŒãžãå¿çã§éä¿¡ããå¿ èŠããããŸãïŒãã®ãããªã¡ãã»ãŒãžã®å Žåãæ倧ãã€ããŒãé·ã¯125ã§ãïŒã ãŸããæåã«pingã¡ãã»ãŒãžãéä¿¡ããªããŠããpongã¡ãã»ãŒãžãåä¿¡ã§ããŸãã ãã®ãããªã¡ãã»ãŒãžã¯åçŽã«ç¡èŠã§ããŸãã
åæ§ã®ã¡ãã»ãŒãžã³ã°ã¹ããŒã ã¯éåžžã«äŸ¿å©ã§ãã ã¢ã€ãã«æ¥ç¶ãåæ¢ãããµãŒãã¹ïŒããŒããã©ã³ãµãŒãªã©ïŒããããŸãã
ããã«ãäžæ¹ã®åœäºè ã¯ãè¿œå ã®åªåãªãã§ã¯ãããäžæ¹ã®åŽãäœæ¥ãå®äºããããšãç¥ãããšãã§ããŸããã 次åããŒã¿ãéä¿¡ãããšãã®ã¿ãäœããããŸããããªãã£ãããšãããããŸãã
ãšã©ãŒåŠç
error
ã€ãã³ãã«ãµãã¹ã¯ã©ã€ãããããšã«ãããWebSocketæ¥ç¶ã§ã®äœæ¥äžã«ãšã©ãŒãåŠçã§ã
error
ã 次ã®ããã«ãªããŸãã
var socket = new WebSocket('ws://websocket.example.com'); // . socket.onerror = function(error) { console.log('WebSocket Error: ' + error); };
æ¥ç¶ãéãã
æ¥ç¶ãéããã«ã¯ãã¯ã©ã€ã¢ã³ããŸãã¯ãµãŒããŒã®ããããã
opcode
ãã£ãŒã«ãã
0x8
èšå®ããŠå¶åŸ¡ãã¬ãŒã ãéä¿¡ããå¿ èŠããããŸãã ãã®ãããªãã¬ãŒã ãåä¿¡ãããšãå察åŽã¯å¿çãšããŠãã¬ãŒã ãéä¿¡ããŠæ¥ç¶ãéããŸãã 次ã«ãæåã®åŽãæ¥ç¶ãéããŸãã ãããã£ãŠãæ¥ç¶ãéããåŸã«ååŸããããŒã¿ã¯ç Žæ£ãããŸãã
ã¯ã©ã€ã¢ã³ãã§WebSocketæ¥ç¶ãéããæäœãéå§ããæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
// , . if (socket.readyState === WebSocket.OPEN) { socket.close(); }
ããã«ãæ¥ç¶ãéããããåŸã«ã¯ãªãŒã³ã¢ããããããã«ã
close
ã€ãã³ãã«ãµãã¹ã¯ã©ã€ãã§ããŸãã
// . socket.onclose = function(event) { console.log('Disconnected from WebSocket.'); };
å¿ èŠã«å¿ããŠããµãŒããŒã¯åŠçããããã«
close
ã€ãã³ãããªãã¹ã³ããå¿ èŠããããŸãã
connection.on('close', function(reasonCode, description) { // . });
WebSocketãšHTTP / 2ãã¯ãããžãŒã®æ¯èŒ
HTTP / 2ã¯å€ãã®æ©èœãæäŸããŸããããã®ãã¯ãããžãŒã¯æ¢åã®ããã·ã¥ãã¯ãããžãŒãšã¹ããªãŒãã³ã°ã¡ãã£ã¢ãå®å šã«çœ®ãæããããšã¯ã§ããŸããã
HTTP / 2ã«ã€ããŠæåã«ç¥ã£ãŠããã¹ãéèŠãªããšã¯ãHTTPãæã€ãã¹ãŠã®ãã®ã«ä»£ãããã®ã§ã¯ãªããšããããšã§ãã èŠæ±ã¿ã€ããã¹ããŒã¿ã¹ã³ãŒããããã³ã»ãšãã©ã®ããããŒã¯ãHTTPã䜿çšããå ŽåãšåããŸãŸã§ãã HTTP / 2ã®é©æ°ã¯ããããã¯ãŒã¯ãä»ããããŒã¿äŒéã®å¹çãé«ããããšã§ãã
HTTP / 2ãšWebSocketãæ¯èŒãããšãå€ãã®é¡äŒŒç¹ãããããŸãã
ã€ã³ãžã±ãŒã¿
| HTTP / 2
| Webãœã±ãã
|
ããããŒå§çž®
| ã¯ãïŒHPACKïŒ
| ãã
|
ãã€ããªããŒã¿è»¢é
| ã¯ã
| ã¯ãïŒãã€ããªãŸãã¯ããã¹ãïŒ
|
å€éå
| ã¯ã
| ã¯ã
|
åªå
é äœä»ã
| ã¯ã
| ãã
|
å§çž®
| ã¯ã
| ã¯ã
|
æ¹å
| ã¯ã©ã€ã¢ã³ã/ãµãŒããŒããã³ãµãŒããŒããã·ã¥
| åæ¹åããŒã¿è»¢é
|
å
šäºé
| ã¯ã
| ã¯ã
|
æ¢ã«è¿°ã¹ãããã«ãHTTP / 2ã«ã¯ãµãŒããŒããã·ã¥ãã¯ãããžãŒãå°å ¥ãããŠããããµãŒããŒã¯ç¬èªã®ã€ãã·ã¢ããã§ã¯ã©ã€ã¢ã³ããã£ãã·ã¥ã«ããŒã¿ãéä¿¡ã§ããŸãã ãã ãããã®ãã¯ãããžãŒã䜿çšããå ŽåãããŒã¿ãã¢ããªã±ãŒã·ã§ã³ã«çŽæ¥éä¿¡ããããšã¯ã§ããŸããã ãµãŒããŒãç¬èªã®ã€ãã·ã¢ããã§éä¿¡ããããŒã¿ã¯ãã©ãŠã¶ãŒã§åŠçãããŸãããããšãã°ããµãŒããŒããã®ããŒã¿ã®åä¿¡ãã¢ããªã±ãŒã·ã§ã³ã«éç¥ããŠãã®ã€ãã³ãã«å¿çããAPIããããŸããã
ãã®ãããªç¶æ³ã§ããµãŒããŒéä¿¡ã€ãã³ãïŒSSEïŒãã¯ãããžãŒãéåžžã«åœ¹ç«ã¡ãŸãã SSEã¯ããµãŒããŒãã¯ã©ã€ã¢ã³ããšãµãŒããŒã®æ¥ç¶ã確ç«ããåŸãéåæã§ããŒã¿ãã¯ã©ã€ã¢ã³ãã«éä¿¡ã§ããããã«ããã¡ã«ããºã ã§ãã
æ¥ç¶åŸããµãŒããŒã¯ãããšãã°æ¬¡ã®ããŒã¿ã転éããæºåãã§ãããšãã«ããã®è£éã§ããŒã¿ãéä¿¡ã§ããŸãã ãã®ã¡ã«ããºã ã¯ãäžæ¹åã®ãããªãã·ã£ãŒ-ãµãã¹ã¯ã©ã€ããŒã¢ãã«ãšããŠæ³åã§ããŸãã ããã«ããã®ãã¯ãããžãŒå ã«ã¯ã
EventSource
ãšåŒã°ããæšæºJavaScriptã¯ã©ã€ã¢ã³ãAPIããããHTML5 W3Cæšæºã®äžéšãšããŠææ°ã®ãã©ãŠã¶ãŒã«å®è£ ãããŠããŸãã EventSource APIããµããŒãããŠããªããã©ãŠã¶ãŒã«ã¯ãããªãã£ã«ãããããšã«æ³šæããŠãã ããã
SSEãã¯ãããžãŒã¯HTTPã«åºã¥ããŠãããããHTTP / 2ãšããŸãçµã¿åããããŸãã ããã€ãã®HTTP / 2æ©èœãšçµã¿åãããŠãè¿œå ã®èŠç¹ãéãããšãã§ããŸãã ã€ãŸããHTTP / 2ã¯å€éåãã£ãã«ã«åºã¥ããå¹ççãªãã©ã³ã¹ããŒãå±€ãæäŸããSSEã¯ãµãŒããŒããããŒã¿ãéä¿¡ããããã®APIãæäŸããŸãã
ããŒã¿ã®å€éåãšã¹ããªãŒãã³ã°ã®æ©èœãå®å šã«ç解ããããã«ãIETFã®å®çŸ©ãèŠãŠã¿ãŸãããã ãã¹ããªãŒã ãã¯ãHTTP / 2æ¥ç¶ã®äžéšãšããŠã¯ã©ã€ã¢ã³ããšãµãŒããŒéã§éä¿¡ãããç¬ç«ããåæ¹åã®ãã¬ãŒã ã·ãŒã±ã³ã¹ã§ãã ãã®äž»ãªç¹åŸŽã®1ã€ã¯ã1ã€ã®HTTP / 2æ¥ç¶ã«åæã«éããŠããè€æ°ã®ã¹ããªãŒã ãå«ããããšãã§ããããšãããã«ããšã³ããã€ã³ããè€æ°ã®ã¹ããªãŒã ããã®äº€äºãã¬ãŒã ãåŠçã§ããããšã§ãã
SSEãã¯ãããžãŒã¯HTTPã«åºã¥ããŠããŸãã ã€ãŸããHTTP / 2ã䜿çšãããšãè€æ°ã®SSEã¹ããªãŒã ã1ã€ã®TCPæ¥ç¶ã§ããŒã¿ãéä¿¡ã§ããã ãã§ãªããSSEã¹ããªãŒã ã®ããã€ãã®ã»ããïŒãµãŒããŒäž»å°ã§ã¯ã©ã€ã¢ã³ãã«ããŒã¿ãéä¿¡ïŒãšè€æ°ã®SSEã¹ããªãŒã ã®çµã¿åããã§ãåãããšãã§ããŸãã¯ã©ã€ã¢ã³ãèŠæ±ïŒãµãŒããŒã«éä¿¡ïŒã
HTTP / 2ãšSSEã®ãããã§ãHTTPã®æ©èœã®ã¿ã«åºã¥ããŠåæ¹åæ¥ç¶ãæŽçã§ããå¯èœæ§ããããã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒããã®ããŒã¿ãåŠçã§ããã·ã³ãã«ãªAPIããããŸãã äžååãªåæ¹åæ©èœã¯ãSSEãšWebSocketãæ¯èŒãããšãã«å€§ããªæ¬ ç¹ãšãªãããšããããããŸããã HTTP / 2ã®ãããã§ããã®æ¬ é¥ã¯ãã¯ãååšããŸããã ããã«ãããWebSocketãã¯ãããžãŒã䜿çšããã«ãHTTPæ©èœã®ã¿ã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒéšåãšã¯ã©ã€ã¢ã³ãéšåã®éã§ããŒã¿ã亀æããã·ã¹ãã ãæ§ç¯ããå¯èœæ§ãéãããŸãã
WebSocketãšHTTP / 2ã äœãéžæããŸããïŒ
HTTP / 2 + SSEãã³ãã«ã¯éåžžã«åºã䜿çšãããŠããŸãããWebSocketãã¯ãããžãŒã¯ãäž»ã«éçºãé²ãã§ãããšããäºå®ãšãéåžžã«ç¹å®ã®ã±ãŒã¹ã§ã¯HTTP / 2ãããå°ãªãè¿œå ã·ã¹ãã è² è·ã§ïŒããšãã°ãããããŒã«é¢ããŠïŒåæ¹åããŒã¿äº€æãæäŸããããã«äœæãããããã
ã¯ã©ã€ã¢ã³ããšãµãŒããŒéã§å€§éã®ã¡ãã»ãŒãžãéä¿¡ããå¿ èŠããããªã³ã©ã€ã³ã²ãŒã ãäœæãããšããŸãã ãã®å ŽåãWebSocketã¯HTTP / 2ãšSSEã®çµã¿åãããããã¯ããã«åªããŠããŸãã
äžè¬ã«ãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®éä¿¡ã®çµç¹ã§ããªã¢ã«ã¿ã€ã ã®ããŒã¿äº€æã«è¿ã¥ããéåžžã«äœã¬ãã«ã®ã¬ã€ãã³ã·ãå¿ èŠãªå Žåã«ã¯ãWebSocketã®äœ¿çšããå§ãããŸãã ãã®ã¢ãããŒãã§ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒéšåã®æ§ç¯æ¹æ³ãåèããå¿ èŠãããããŸããããã§ã¯ã€ãã³ããã¥ãŒãªã©ã®ä»ã®ãã¯ãããžãŒã«æ³šæãæãå¿ èŠããããããããªããšããäºå®ãèŠããŠãããŠãã ããã
ããšãã°ããŠãŒã¶ãŒã«ãªã¢ã«ã¿ã€ã ã®ãã¥ãŒã¹ãåžå ŽããŒã¿ã衚瀺ããå¿ èŠãããå ŽåããŸãã¯ãã£ããã¢ããªã±ãŒã·ã§ã³ãäœæããŠããå ŽåãHTTP / 2 + SSEãã³ãã«ã䜿çšãããšãå¹æçãªåæ¹åéä¿¡ãã£ãã«ãåŸãããåæã«äžçã®ãã¯ãããžãŒã䜿çšããå©ç¹ãåŸãããŸãHTTP , WebSocket , -, HTTP- , HTTP . , . - (, , ) , HTTP. , , , HTTP-.
, . , WebSocket:
. , HTTP/2 :
HTTP/2 :
- HTTP/2 TLS (, , ).
- IE 11, Windows 10.
- OSX 10.11+ Safari.
- HTTP/2 , ALPN ( ).
SSE, , :
IE/Edge. (, Opera Mini SSE, WebSocket, , , .) , IE/Edge .
ãŸãšã
, WebSockets HTTP/2+SSE , , , . - ? , . , , . , , , SessionStack, , , WebSockets, HTTP.
SessionStack -, DOM, , , JS-, , , , , . -. . SessionStack HTTP, ( ). WebSocket , . , SessionStack, -, WebSocket, , WebSocket , HTTP.
SessionStack. , , , , , , WebSocket.
芪æãªãèªè ïŒ WebSocket HTTP/2+SSE? â , , , , .