JavaScript

163 kenu
2012-03-20 14:21:55
http://www.okjsp.pe.kr
socket.io ½ÃÀÛÇϱâ

node.js¿Í npmÀÌ ¼³Ä¡µÈ »óÅ¿¡¼­ npm install socket.io ·Î ½ÃÀÛÇÕ´Ï´Ù.
±× ´ÙÀ½¿£ ¾î¼¶ó°í ÀÔ´Ï´Ù.

¼³¸íÀº http://socket.io »çÀÌÆ®°¡ Àִµ¥, Ãʺ¸ÀÚ¿¡°Ô´Â ºÒÄ£ÀýÇÕ´Ï´Ù.
Á൵ ¸ø ¸Ô°Ú½À´Ï´Ù.

±×·¡¼­ ½±°Ô Á¤¸®¸¦ ÇÏ·Á°í Outsider´Ô Ã¥º¸¸é¼­ Á¤¸®ÇÏ·Á°í ÇÕ´Ï´Ù.
¼Ò½º°¡ ÀÖ½À´Ï´Ù. ¾Æ¹« À̽´¾ø½À´Ï´Ù. ^^b

Ã¥ÀÇ Ã¹ ¿¹Á¦´Â ±â¸¦ Á×ÀÔ´Ï´Ù. ÇÑ 199 ÆäÀÌÁö°¡ ´Ù ¼Ò½º.
±×·¡¼­ ½ÃÀÛÀº À§¿¡¼­ ºÒÄ£ÀýÇÏ´Ù°í ¿åÇÑ http://socket.io/#how-to-use ·Î ½ÃÀÛÇÕ´Ï´Ù.

npm install socket.io °¡ µÇ¸é

app.js ÆÄÀÏÀ» ¸¸µì´Ï´Ù. ¼­¹ö¿¡¼­ µ¹¾Æ°¡´Â javascriptÀÔ´Ï´Ù.
----
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

----

±×¸®°í Ŭ¶óÀÌ¾ðÆ®¿¡¼­ µ¹¾Æ°¡´Â index.html ÀÔ´Ï´Ù.
----
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
----

Æú´õ¸¦ º¸¸é ´ÙÀ½°ú °°ÀÌ ÀÖ½À´Ï´Ù.
app.js
index.html
node_modules/

ÀÌÁ¦ ´ÙÀ½ ¸í·É¾î·Î ¼­¹ö¸¦ ¶ç¿ì¸é µË´Ï´Ù.
node app.js

80Æ÷Æ®·Î ¶ß±â ¶§¹®¿¡ root±ÇÇÑÀÌ ÇÊ¿äÇÕ´Ï´Ù. ±ÇÇÑÀÌ ¾ø´Â °æ¿ì ´ÙÀ½°ú °°Àº ¿¡·¯°¡ ¹ß»ýÇÕ´Ï´Ù.
gwang-nam-heoyi-macbook-pro:socket.io kenu$ node app.js
info - socket.io started
warn - error raised: Error: listen EACCES
gwang-nam-heoyi-macbook-pro:socket.io kenu$

 
sudo node app.js 
¸®´ª½º¿Í ¸ÆÀº ÀÌ·¸°Ô ½ÇÇàÇÏ¼Å¾ß ÇÒ °ÍÀÔ´Ï´Ù.


¼­¹öÂÊ¿¡ ´ÙÀ½°ú °°ÀÌ ·Î±×°¡ ¿Ã¶ó¿É´Ï´Ù.

gwang-nam-heoyi-macbook-pro:socket.io kenu$ sudo node app.js
Password:
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 21130281791178129285
debug - setting request GET /socket.io/1/websocket/21130281791178129285
debug - set heartbeat interval for client 21130281791178129285
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
{ my: 'data' }
±×¸®°í Ŭ¶óÀÌ¾ðÆ® ºê¶ó¿ìÀúÀÇ ÄܼÖÀ» º¸¸é ´ÙÀ½°ú °°Àº ÄÜ¼Ö ·Î±×¸¦ º¼ ¼ö ÀÖ½À´Ï´Ù.

Object
hello: "world"
__proto__: Object

¼­¹öÀÇ ¸Þ½ÃÁö{"hello":"world"}°¡ Ŭ¶óÀÌ¾ðÆ®·Î, Ŭ¶óÀ̾ðÆ®ÀÇ ¸Þ½ÃÁö{my:'data'}°¡ ¼­¹ö·Î Àü¼ÛµÇ¼­ ·Î±×¿¡ ÂïÈù °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù.

¼­¹öÀÇ ·Î±×¸¦ º¸½Ã¸é °è¼Ó ÁÖ±âÀûÀ¸·Î ½ÉÀå¹Úµ¿(heartbeat)À» È®ÀÎÇÏ´Â ·Î±×°¡ ¿Ã¶ó°¡´Â °ÍÀ» º¼ ¼ö ÀÖ½À´Ï´Ù.

Å©·Ò ºê¶ó¿ìÀú Console¿¡¼­ socket.emit('my other event', {my:'okjsp message'}); ÀÔ·ÂÇÏ¸é ¼­¹öÂÊ¿¡ Àü¼ÛµÈ ¸Þ½ÃÁö ·Î±×°¡ ÂïÈü´Ï´Ù.

ÀÌÈÄ´Â Outsider´ÔÀÇ Ã¥À» Âü°í·Î ÇØ¼­ ¿øÇÏ´Â ½ÃÁ¡¿¡ ¸Þ½ÃÁö¸¦ ÁÖ°í ¹ÞÀ» ¼ö ÀÖµµ·Ï Äڵ带 °³¼±½ÃÄÑ ³ª°¡º¸°Ú½À´Ï´Ù.
to be continued...

  • °£´ÜÇÑ °ÅÁö¸¸ http://socketio-example.nodester.com/ ¸¦ º¸¼Åµµ Åë½ÅÀ» ¾î¶»°Ô ÇÏ´ÂÁö µµ¿òÀÌ µÇ½Çµí ÇÕ´Ï´Ù. nodester°¡ ºÒ¾ÈÇØ¼­ Á¾Á¾ Á×³×¿ä ¤¾¤¾ ¾÷±Û¾ÈÇØ¼­ Á» ¿¹Àü¹öÀüÀ̱ä ÇÏÁö¸¸¿ä
  • 4796Outsider
  • 2012-03-20 13:56:13
  • x
  • Outsider//°¨»çÇÕ´Ï´Ù. ^^; ¸ð´ÏÅ͸µ ÇϽô °Í °°¾Æ¿ä.
  • 163kenu
  • 2012-03-20 14:03:17
  • x
  • kenu´Ô// ¤¾¤¾¤¾¤¾ node.js ¼³¸íÇÏ½Ã´Â°É °Åµé»ÓÀÌÁÒ ¤¾¤¾
  • 4796Outsider
  • 2012-03-20 15:29:07
  • x
  • ¿¹Á¦»çÀÌÆ® ÀÌÇØ¸¦ Çϱ⿡ ÁÁÀº »ùÇõéÀ̳׿ä. °¨»çÇÕ´Ï´Ù.
  • 163kenu
  • 2012-03-20 15:34:22
  • x
  • ¿ÀÈ£ ¼ÒÄÏ ÇÁ·Î±×·¥ ½¬°Ô µÇ³×
  • 11674³ëÃ÷°³¹ß
  • 2012-03-21 15:19:28
  • x
  • emit À¸·Î º¸³»°í on À¸·Î ¹Þ°í ÀÌ µÎ°¡Áö¸¸ »ý°¢ÇÏ¸é µÇ¿ä. ( ºÏÄ¡±â ¹ÚÄ¡±â ó·³..)
  • 17196inspiredjw
  • 2012-03-22 10:49:22
  • x
´ÙÀ½±Û À©µµ¿ì7¿¡ express ¼³Ä¡Çϱâ
>> 141 socket.io ½ÃÀÛÇÏ±â   [6] kenu 2837 2012-03-20 14:21:55
ÀÌÀü±Û java RESTful ¼­ºñ½º¸¦ node.js·Î ¿Å±ä À̾߱â