æ代é ãã²ã¨ãFizzBuzzç¥ããä»åãJavaScript de å¦çç³»ãã¿ãæè¿æµè¡ã®Node.jsã触ã£ã¦ã¿ãããµã¼ããµã¤ãï¼ãååã®Rhinoã¯ãã®è¡ççã«å¾®å¦ã«ã¯ã©ã¤ã¢ã³ããµã¤ãã®æµããæ±²ãã§ããããã«æã*1ãã©ãããã¨ã¯å¯¾ç §çã *2ã
å人çã«ããµã¼ããµã¤ãJavaScriptãæåãããå¦ãã¯å¥ã¨ãã¦ãNode.jsã«ã¯ä¸å¦çç³»ã¨ãã¦åºã¾ã£ã¦æ¬²ããã¨æã£ã¦ãããä½ããJavaScriptã¯Windows Script Host以å¤ã«ãã¼ã«ã«ã§ããã¹ãå¦çãããããæã«ä½¿ããã¡ã¸ã£ã¼ãªå¦çç³»ãå°ãªããPerlãRubyã§ä½¿ãæ¨ã¦ãã¼ã«ãæ¸ããããªã·ãã¥ã¨ã¼ã·ã§ã³ã§Node.jsã使ãããããã«ãªãã¨çµæ§é¢ç½ãã¨æãã®ã ã
ä»åã¯v0.6.15ã®Windowsçãã¤ããªã使ç¨ãããä¸é¨v0.7.8ã使ã£ã¦ããã
æ®éã®FizzBuzz
æ®éã«FizzBuzzããã¨ãä¾ãã°ãããªæãã ãããï¼
/*jslint devel: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); console.log(iota(100, 1).map(fizzbuzz).join('\n')); }());
æ¨æºåºåãæ¨æºã¨ã©ã¼åºåã使ãçºã«consoleã¢ã¸ã¥ã¼ã«ãç¨æããã¦ãããè¦ãç®ã¯ã¯ã©ã¤ã¢ã³ããµã¤ãJavaScriptã§consoleãªãã¸ã§ã¯ãã使ãã®ã¨å¤ãããªãã
ã¢ã¸ã¥ã¼ã«æ©è½
Node.jsã¯ãµã¼ããµã¤ãã®å¦çç³»ãªã®ã§ãã¢ã¸ã¥ã¼ã«åã®ææ³ã¯ã¯ã©ã¤ã¢ã³ããµã¤ãã¨ã¯ç°ãªãã
/*jslint node: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); exports.answers = function (count) { if (typeof count !== 'number') { throw new TypeError('fizzbuzz.answers(): count must be number'); } if (count <= 0) { throw new RangeError('fizzbuzz.answers(): count must be > 0'); } return iota(count, 1).map(fizzbuzz); }; }());
`exports.answers' ã®è¾ºããã¢ã¸ã¥ã¼ã«ã®å ¬éé¨ãå ¨ä½ãç¡åé¢æ°ã§å²ã£ã¦ãããã©ãã©ããNode.jsã§ã¯é¢æ°ã®å¤ã§å¤æ°ãå®ç¾©ãã¦ãã¢ã¸ã¥ã¼ã«ï¼ã¨ããããã¡ã¤ã«ï¼ï¼ã®å¤ã«ã¯å ¬éãããªããããã
ãã®ã¢ã¸ã¥ã¼ã«ã fizzbuzz.js ã¨ãããã¡ã¤ã«ã§å®ç¾©ããã¦ããã¨ããã¨ããã®ä½¿ãæ¹ã¯ä¾ãã°ãããªæãã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var fizzbuzz = require('./fizzbuzz.js'); fizzbuzz.answers(100).forEach(function (v) { console.log(v); }); }());
ä¸æ¦å¤æ°fizzbuzzã§åãã¦ãããããã®è¾ºãã¯å¥½ã好ãã ããã
readline
å人çã«ã¯åãªãå¦çç³»ã¨ãã¦ã®Node.jsã«ãæå¾ ããæãããã
ä¾ãã°Node.jsã§ã®å ¥åã¹ããªã¼ã ã®æ±ãã¯ãé©å½ãªå¤§ããã®ãã£ã³ã¯ãéåæã«ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã¨ãã¦æ¸¡ããããã¨ãããã¿ã¼ã³ãå¤ãã®ã ãã©ãä½ããã®ããã¹ããã¼ã¿ãå¦çãããå ´åã¯å ¥åããé©å½ãªå¤§ããã®ãã£ã³ã¯ããããã1è¡ãã§ããæ¹ãé½åããããã¨ãå¤ã*3ã
ã³ã³ã½ã¼ã«ã¨ãããttyããã®ã¦ã¼ã¶å ¥åã«é¢ãã¦ã¯ãreadlineã¢ã¸ã¥ã¼ã«ã使ããã¨ã§æ軽ã«1è¡ãã¨ã«ãã¼ã¿ãæ±ããã¨ãã§ããã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var readline = require('readline'), rl = readline.createInterface(process.stdin, process.stdout), fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); rl.on('line', function (line) { var count = line.trim(); if (count === '') { // EMPTY } else if (/^[1-9][0-9]{0,8}$/.test(count)) { console.log(fizzbuzz(count)); } else { console.error("Invalid input: '%s'", line); } }).on('close', function () { process.exit(0); }).resume(); }());
æ¨æºå ¥åããã®ã¦ã¼ã¶å ¥åã«å¯¾ãã¦FizzBuzzã®çããè¿ãå®è£ ã®ä¾ãC-cãªããC-dã§çµäºããã
ãã®å®è£ ã¯ã¦ã¼ã¶ãæå ¥åããåã«ã¯åé¡ãªãããä¾ãã°seqã§çæããæ¹è¡åºåãã®æ°åããã¤ãçµç±ã§æµãè¾¼ãã ããåã¯æ°åãè¨è¿°ããããã¹ãããªãã¤ã¬ã¯ãã§æµãè¾¼ãã ããããã¨ããã¨ãv0.6.15ã§ã¯ãããªå®è¡æã¨ã©ã¼ãçºçããã
node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ AssertionError: stdin must be initialized before calling setRawMode
v0.7.8ã§ã¯å®è¡æã¨ã©ã¼ã¯çºçããªãã®ã ããä½æ ãå ¥åå¤èªä½ãã³ã³ã½ã¼ã«ã«ã¨ã³ã¼ããã¯ããã¦ãã¾ããç´æçã«ã¯å ¥åèªä½ã¯ã¨ã³ã¼ããã¯ãããã«FizzBuzzã®çãã®é¨åã ããåºåãããããã«æãã®ã ãâ¦â¦ã
ãã®å¾åã¯ãä¾ãã°fsã¢ã¸ã¥ã¼ã«ã使ã£ã¦ãã¡ã¤ã«ãããã¼ã¿ãèªã¿åºãããã«ãã¦ãå¤ãããªãã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var fs = require('fs'), readline = require('readline'), fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()), input, rl; input = fs.createReadStream('./fizzbuzz5.txt', { encoding: 'utf8' }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); process.exit(1); }); input.pause(); rl = readline.createInterface(input, process.stdout); rl.on('line', function (line) { var count = line.trim(); if (count === '') { // EMPTY } else if (/^[1-9][0-9]{0,8}$/.test(count)) { console.log(fizzbuzz(count)); } else { console.error("Invalid input: '%s'", line); } }).on('close', function () { process.exit(0); }).resume(); }());
ããã®æåãv0.6.15ãv0.7.8å ±ã«åã®ãã¼ã¸ã§ã³ã¨åãã«ãªãã
ãã¯ãreadlineã¯ttyã®å ¥åºåå°ç¨ãã¨ãããã¨ã ãããï¼ãããå°ãæ±ç¨çã«å ¥åãè¡åä½ã«æ±ããä»çµã¿ã欲ããã
HTTPã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãï¼GETï¼
ãµã¼ããµã¤ãJavaScriptã®å¦çç³»ã¨ãããã¨ããããHTTPã®ã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãã¨ãã¦æ¯èãã¢ããªãæ¸ãã®ã¯æ¯è¼çç°¡åã ã
ä¾ãã°æå®ããæ°ã ãFizzBuzzã®çãããµã¼ãããåå¾ããHTTPã¯ã©ã¤ã¢ã³ãã¯ãããªé¢¨ã«æ¸ããã¨ãã§ããã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var options = { host: 'localhost', port: 5432, path: '/fizzbuzz?count=100' }; require('http').get(options, function (response) { if (response.statusCode === 200) { response.pipe(process.stdout); response.resume(); } else { console.error('status: %d', response.statusCode); } }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }); }());
GETã¡ã½ããã使ç¨ããURLã«ãã©ã¡ã¼ã¿ãä»å ãã¦ãããä»åã¯ãã©ã¡ã¼ã¿ãç´æ¸ããã¦ãããã©ãå¿ è¦ã«å¿ãã¦querystring.stringifyãªã©ã使ãã¹ãã ããããµã¼ãããã®ã¬ã¹ãã³ã¹ã¯ãã®ã¾ã¾æ¨æºåºåã«åãæµãããã«ãã¦ããã
ãããã¾ã使ãæ¨ã¦HTTPã¯ã©ã¤ã¢ã³ãããã£ã¨æ¸ãç¨åº¦ãªãä»ã®ã¹ã¯ãªããè¨èªã§ãåé¡ãªããæ¨æºã©ã¤ãã©ãªã«HTTPã¯ã©ã¤ã¢ã³ãã»ã©ã¤ãã©ãªã®é¡ãç¨æããã¦ãããã®ã ã
Node.jsãå¤ãã£ã¦ããï¼å¤±ç¤¼ï¼ã®ã¯ããµã¼ããµã¤ãJavaScriptã®å¦çç³»ãªã ãã«HTTPãµã¼ãçãªæ¯ãèããããããã°ã©ã ãæ¸ãçºã®ã¢ã¸ã¥ã¼ã«ãæ¨æºã§ç¨æããã¦ãããã¨ã ã
/*jslint node: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var url = require('url'), hasOwn = Object.prototype.hasOwnProperty, have = function (hash, key) { return hasOwn.call(hash, key); }, iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); require('http').createServer(function (request, response) { var requrl, query, count; if (request.method !== 'GET') { response.statusCode = 405; response.end(); return; } response.statusCode = 400; requrl = url.parse(request.url, true); if (!have(requrl, 'pathname') || (requrl.pathname !== '/fizzbuzz')) { response.end(); return; } if (!have(requrl, 'query')) { response.end(); return; } query = requrl.query; if (!have(query, 'count') || !/^[1-9][0-9]{0,8}$/.test(query.count)) { response.end(); return; } count = Number(query.count); console.assert(isFinite(count)); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(iota(count, 1).map(fizzbuzz).join('\n') + '\n'); }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }).listen(5432); }());
å ã»ã©ã®ã¯ã©ã¤ã¢ã³ãã«å¯¾ãããµã¼ãå´ã®å®è£ ä¾ãHTTPã¨ãã¦ããªããã£ããªæ©è½ã°ãããªã®ã§æ¬æ ¼çãªWebã¢ããªãæ¸ãã«ã¯åããªã*4ãã®ã®ãããã¼ã®CGIãã©ããæ¨æºã®æ©è½ã®ã¿ã§æ¯è¼çæ軽ã«æ¸ããç¹ã¯ãããã¨æãã
HTTPã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãï¼POSTï¼
HTTPã¯ã©ã¤ã¢ã³ããGETã¡ã½ããã使ããã¿ã¼ã³ã¯å¤ãã®ã§http.getã¨ããå°ç¨ã®ã¡ã½ãããç¨æããã¦ãããä¸æ¹ãGET以å¤ã®ã¡ã½ããã使ãããå ´åã¯http.requestã¨ããæ±ç¨ã®ã¡ã½ããã使ç¨ãããªãã·ã§ã³ã§ä½¿ç¨ãããã¡ã½ãããæå®ãããã¨ã«ãªãã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var options = { method: 'POST', host: 'localhost', port: 5432, path: '/fizzbuzz.cgi' }; require('http').request(options, function (response) { if (response.statusCode === 200) { response.pipe(process.stdout); response.resume(); } else { console.error('status: %d', response.statusCode); } }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }).end('count=100'); }());
POSTã使ç¨ããå ã»ã©ã¯URLã«ä»å ãã¦ãããã©ã¡ã¼ã¿ããªã¯ã¨ã¹ãããã£ã§éä¿¡ããããã«ãã¦ã¿ãã
ãªã¯ã¨ã¹ãã«ããã¬ã¹ãã³ã¹ã«ãããã¡ãã»ã¼ã¸ããã£ãåãåãéã«ã¯æ³¨æãå¿ è¦ã ãã¨ããã®ãã¤ãã³ã `data' ã®å¼æ°ã¨ãã¦å¼ã渡ããããã¼ã¿ã¯ãã¡ãã»ã¼ã¸ããã£ã®æçããªã®ã§ãå ¨ãã¼ã¿ãä¸åº¦ã« or 1è¡ãã¨ã«åãåããä¿è¨¼ã¯ãªãã
/*jslint node: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var querystring = require('querystring'), hasOwn = Object.prototype.hasOwnProperty, have = function (hash, key) { return hasOwn.call(hash, key); }, iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); require('http').createServer(function (request, response) { var body; if (request.method !== 'POST') { response.statusCode = 405; response.end(); return; } if (request.url !== '/fizzbuzz.cgi') { response.statusCode = 404; response.end(); return; } request.setEncoding('utf8'); body = []; request.on('data', function (chunk) { body.push(chunk); }); request.on('end', function () { var query = querystring.parse(body.join('')), count; if (!have(query, 'count') || !/^[1-9][0-9]{0,8}$/.test(query.count)) { response.statusCode = 400; response.end(); return; } count = Number(query.count); console.assert(isFinite(count)); response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(iota(count, 1).map(fizzbuzz).join('\n') + '\n'); }); }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }).listen(5432); }());
POSTçã¯ã©ã¤ã¢ã³ãã«å¯¾å¿ãããµã¼ãå´ã®å®è£ ä¾ããªã¯ã¨ã¹ãããã£ãåä¿¡ãã度ã«ãã£ã³ã¯ãé åã«è©°ãã¦ãããåä¿¡çµäºæã«é£çµãã¦ãã解æãå§ããããã«ãã¦ããã
ãããæ¸ãã¦ãã¦é¢ç½ãã¨æã£ãã®ã¯ãã¯ãã¼ã¸ã£ã使ããã¨ã§ãªã¯ã¨ã¹ãåä½ã§åãªãã¸ã§ã¯ããã¾ã¨ãã¦ãã¾ããæã ããã®ä¾ã§ã¯å¤æ°bodyã¯ãªã¯ã¨ã¹ããã¨ã«ä¿æããå¿ è¦ãããã®ã ããã¯ãã¼ã¸ã£ã«ãã£ã¦ã»ã¨ãã©æèãããã¨ãªããããå®ç¾ã§ãã¦ããã
Node.jsã®HTTPåãã®æ©è½ã¯çµæ§å å®ãã¦ãããhttpã¢ã¸ã¥ã¼ã«ã¨ã»ã¼åãã¤ã³ã¿ã¼ãã§ã¼ã¹ãæã¤httpsã¢ã¸ã¥ã¼ã«ãæ¨æºã§ä»ãã¦ãããããµã¼ããã¼ãã£ã®ã¢ã¸ã¥ã¼ã«ã«ã¯WebSocketãWebãã¬ã¼ã ã¯ã¼ã¯ãæä¾ãããã®ãããããããã使ããªãã¦ãç°¡åãªä½¿ãæ¨ã¦ãã¼ã«ã®é¡ãªãHTTPã¯ã©ã¤ã¢ã³ãããµã¼ãã®ã©ã¡ãå´ãçµæ§æ軽ã«æ¸ãããç¹ã«ãµã¼ãçãªä½¿ãæ¨ã¦ã¢ããªãæ軽ã«æ¸ãã¦ä¸ã¤Webãµã¼ããä¸è¦ãªï¼Node.jsåä½ã§åä½ããï¼äºã¯ãå人çã«ã¯ãã¤ã³ããé«ãã
TCPã¯ã©ã¤ã¢ã³ãï¼ãµã¼ã
HTTPããä¸å±¤ã®ãããã³ã«ã使ãããä¾ãã°TCPã«ããã¯ã©ã¤ã¢ã³ãã¯ãããªæãã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var socket; socket = require('net').createConnection(5432, 'localhost'); socket.on('connect', function () { socket.setTimeout(10000); socket.end('100'); socket.pipe(process.stdout); }).on('timeout', function () { console.error('error: connection timeout'); socket.destroy(); }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); socket.destroy(); }); }());
Cè¨èªã§ã½ã±ããããã°ã©ãã³ã°ãããã¨ããã身ã¨ãã¦ã¯ãTCPã½ã±ããã£ã½ãé¨åãè²ã ã¨ããã®ã«JavaScriptçãªç¡åé¢æ°ã«ããã³ã¼ã«ããã¯ãå¤ç¨ããã¹ã¿ã¤ã«ã«ãªã£ã¦ããã®ã§ä¸æè°ãªæãã ã
ä»åº¦ã¯ãµã¼ãå´ãã¯ã©ã¤ã¢ã³ãå´ãããã ã£ããHTTPãã¼ã¸ã§ã³ã¨ä¼¼ããããªæ¸ãæ¹ã«ãªã£ã¦ããã
/*jslint node: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var util = require('util'), iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n; return ref[fizz + buzz]; }; }()); require('net').createServer({ allowHalfOpen: true }, function (socket) { var body = []; socket.setEncoding('utf8'); socket.setTimeout(30000); socket.on('data', function (chunk) { body.push(chunk); }).on('timeout', function () { socket.destroy(); }).on('error', function (e) { socket.destroy(); }).on('end', function () { var input = body.join(''), count = input.trim(); if (/^[1-9][0-9]{0,8}$/.test(count)) { count = Number(count); console.assert(isFinite(count)); socket.end(iota(count, 1).map(fizzbuzz).join('\n') + '\n'); } else { socket.end(util.format("Invalid input: '%s'\n", input)); } }); }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }).listen(5432, 'localhost'); }());
HTTPã使ããã®ã§ç´æ¥TCPã§éåä¿¡ããæ©ä¼ã¯å°ãªãã¨æãããåºæ¬çã«ã¹ããªã¼ã ãªæã¯HTTPã¨åããªã®ã§å¼·ãéåæãæãããã¨ãªãå®è£ ã§ããã®ã§ã¯ãªãã ãããï¼
UDPã¯ã©ã¤ã¢ã³ãï¼ãµã¼ã
UDPã使ãããã©ãå®éã«ä½¿ãæ©ä¼ã¯å°ãªãã¨æããä¾ãã°ãã±ããã®å°çé ã¯ä¿è¨¼ããã¦ããªãããéä¸ã§æ¶å¤±ãã¦ãã¾ãå¯è½æ§ããããTCPã¯å é¨ã§è²ã ã¨é å¼µã£ã¦ãããã©UDPã§ã¯ã¦ã¼ã¶ï¼ããã°ã©ãï¼ã®è²¬ä»»ã«ãªãã®ã§ããããªãã«å質ã確ä¿ãããã¨ããã¨å®è£ ã大å¤ã«ãªãã
/*jslint node: true, plusplus: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var QU = new Buffer('QU'), TIME_OUT = 10000, iota = function (count, start, step) { var retval = [], i; if (start === undefined) { start = 0; } if (step === undefined) { step = 1; } for (i = 0; i < count; ++i) { retval[i] = start + step * i; } return retval; }, socket, query = function (n) { var msg = new Buffer(QU.length + 4); QU.copy(msg); msg.writeUInt32BE(n, QU.length); socket.send(msg, 0, msg.length, 5432, 'localhost'); }, timeout = function () { console.error('error: timeout'); socket.close(); }, index, timerId; socket = require('dgram').createSocket('udp4'); socket.on('message', function (msg, rinfo) { var status, len; if (msg.length < 2) { console.error('?'); return; } status = msg.toString('utf8', 0, 2); if (status !== 'OK') { console.error('%s', status); return; } if (msg.length < 4) { console.error('error: packet NG'); return; } len = msg.readUInt8(2); if ((3 + len) > msg.length) { console.error('error: packet NG'); return; } console.log('%s', msg.toString('utf8', 3)); ++index; clearTimeout(timerId); if (index <= 100) { timerId = setTimeout(timeout, TIME_OUT); query(index); } else { socket.close(); } }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); socket.close(); }); index = 1; timerId = setTimeout(timeout, TIME_OUT); query(index); }());
UDPçã¯ã©ã¤ã¢ã³ãã®å®è£ ä¾ãå¹¾ã¤ãã®è¦å ãéãªã£ãçºã«TCPçãããã³ã¼ããé·ãè¤éã«ãªã£ã¦ããããã®å®è£ ã§ã¯ãã±ããã1ã¤ã§ãæ¶å¤±ããã¨å¦çãçµäºãã¦ãã¾ãã®ãé£ç¹ã ãããï¼
ãµã¼ãçã®å®è£ ããTCPçãããå½¹å²ãæ¸ã£ãå²ã«ã¯ã³ã¼ããé·ãã ã¨æãã
/*jslint node: true, maxerr: 50, indent: 2 */ (function () { 'use strict'; var OK = new Buffer('OK'), NG = new Buffer('NG'), fizzbuzz = (function () { var ref = [null, 'Fizz', 'Buzz', 'FizzBuzz']; return function (n) { var fizz = n % 3 === 0 ? 1 : 0, buzz = n % 5 === 0 ? 2 : 0; ref[0] = n.toString(); return ref[fizz + buzz]; }; }()), socket; socket = require('dgram').createSocket('udp4'); socket.on('message', function (msg, rinfo) { var reply = function (msg) { socket.send(msg, 0, msg.length, rinfo.port, rinfo.address); }, num, fb, body; if (msg.length !== 6) { reply(NG); return; } if (msg.toString('utf8', 0, 2) !== 'QU') { reply(NG); return; } num = msg.readUInt32BE(2); if (num === 0) { reply(NG); return; } fb = new Buffer(fizzbuzz(num)); body = new Buffer(OK.length + 1 + fb.length); OK.copy(body); body.writeUInt8(fb.length, OK.length); fb.copy(body, OK.length + 1); reply(body); }).on('error', function (e) { console.error('error: [%s] %s', e.name, e.message); }).bind(5432, 'localhost'); }());
ãã¤ããªãã¼ã¿ã®æ±ããã©ãã«ãåé·æ°å³ã«æããã
å®éã®æãUDPãé¸æããã®ã¯æ¬¡ã®æ¡ä»¶ã«å½ã¦ã¯ã¾ãã±ã¼ã¹ãå¤ãã¨æãã
- TCPã®æåãç¹ã«ãã±ããæ¶å¤±æã®åéãåé¡ã«ãªãå ´åã
- ãã±ãããå¤å°æ¶å¤±ãã¦ã大ä¸å¤«ããåã¯åé以å¤ã®æ¹æ³ã§ã«ãã¼ã§ããå ´åã
ä¸è¬çã«ã¯é³å£°ãåç»çµ¡ã¿ã ãããï¼ãå°ãªãã¨ãFizzBuzzã«ã¯TCPã®æ¹ãåãã¦ããã
ã¾ã¨ã
Node.jsã¯èå³æ·±ããä½åº¦ãæ¸ãã¦ãããã©ããµã¼ããµã¤ãJavaScriptäºã ãæãã«ãã¦ãã¯ã©ã¤ã¢ã³ããµã¤ãJavaScript以å¤ã®å®ç¨çãªã¹ã¯ãªããå¦çç³»ã¨ãã¦ä¾¡å¤ãããã¨æããè¨èªãJavaScriptã ãã³ã¼ã«ããã¯ãå¤ç¨ããç¬ç¹ã®ã¹ã¿ã¤ã«ã ããPerlãRubyãªã©ã¨æ¯è¼ããã°çãå¼·ãæ°ãããããããä½ã¨ãããçã®å¼·ãæ ã«ããã人ã¯ãããã¨ã§ããããããCã®ç®ã被ã£ãLispãã¨ããç°åã¯ä¼éã§ã¯ãªãã¨ãããâ¦â¦ã
ç§å人ã¨ãã¦ã¯ãWSHã§ãã¼ã«ãä½ããªãVBScriptãããJScriptãé¸ã¶ãã¨ããç¨åº¦ã«ã¯JavaScriptã好ããªè¨³ã§ãWindows以å¤ã®ç°å¢ã§ããããã£ãå°ãã¼ã«ä½æç¨ã«ä½¿ããå¦çç³»ãåºã¾ã£ã¦ãããã®ã¯æªã話ã§ã¯ãªãã
ã¨ã¯ããæ®åããã«ã¯ãã©ã¼ã¢ããªã¨ãããä½ãããã®ç®çãå¿ è¦ãªè¨³ã§ãç¾ç¶ã§ã¯ããããWebSocketã§ãµã¼ããµã¤ãJavaScriptããªã®ã ã¨æããã©ãæ£ç´ãªã¨ããã©ããªã®ã ããï¼ãæ¢åã®ãµã¼ããµã¤ãç¨è¨èªãªãã©ã¤ãã©ãªãªããWebSocketã«å¯¾å¿ãã¦ãã¾ã£ãããªã·ãã¤ãªæ°ãããã®ã ããéå¤æ¼¢ãªã®ã§ããåãããªãã
*1:SpiderMonkeyã®çµã¿è¾¼ã¿å ãçµã¿è¾¼ã¿å ãªã ãã«ããããå½ç¶ãªããRhinoèªä½ã¯ã¯ã©ã¤ã¢ã³ããµã¤ãJavaScriptã®ç°å¢ã§ã¯ãªãã
*2:ââããæã£ã¦ããã®ã¯ç§ã ãï¼
*3:ããã¯å¦çã®é½åãããããªãããå®è£ ããå´ã®æ £ãã®åé¡ãããããªãã
*4:å¥éãã¬ã¼ã ã¯ã¼ã¯ãç¨æããã¹ãã