회사에서 본격적으로 개발들어가기전에
필요 할 것 같은.. 몇가지 기능들에 대해서 작성한 샘플 코드.
1. Socket.io - 네임스페이스
- 필요 모듈 express (npm install express -g)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express.createServer().listen(9000); | |
var io = require('socket.io').listen(app); | |
io.configure(function() { | |
io.enable('browser client etag'); | |
io.set('log level', 2); | |
io.set('transports', [ | |
'websocket' | |
,'flashsocket' | |
,'htmlfile' | |
,'xhr-polling' | |
,'jsonp-polling' | |
]); | |
}); | |
var gameSocket = io.of('/game'); | |
var chatSocket = io.of('/chat'); | |
gameSocket.on('connection', function(socket) { | |
console.log('aaaaaa'); | |
socket.send('hey! it\'s game channel!'); | |
}); | |
chatSocket.on('connection', function(socket) { | |
console.log('bbbbb'); | |
}); | |
//client | |
$(window).load(function() { | |
socket = io.connect('http://localhost:9000', { | |
'reconnect':true | |
,'resource':'socket.io' | |
}); | |
var another = io.connect('http://localhost:9000/game'); | |
another.on('message', function(msg) { | |
alert(msg); | |
}); | |
}); |
2. mysql
: 필요모듈 mysql (npm install mysql -g)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _mysql = require('mysql'); | |
var host = '127.0.0.1'; | |
var port = '3306'; | |
var mysql_user = 'scott'; | |
var mysql_pass = 'tiger'; | |
var database = 'database'; | |
var mysql = _mysql.createClient({ | |
host: host, | |
port: port, | |
user: mysql_user, | |
password: mysql_pass, | |
}); | |
mysql.query('use ' + database); | |
mysql.query('select name from Label', function(err, result, fields) { | |
console.log('aaaa'); | |
for(var i in result) { | |
var gadget = result[i]; | |
console.log(gadget.name + ' : ' + gadget.name); | |
} | |
}); |
: 필요모듈 (npm install generic-pool -g)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var poolModule = require('generic-pool'); | |
var pool = poolModule.Pool({ | |
name : 'mysql', | |
create :function(callback) { | |
var Client = require('mysql').Client; | |
var c = new Client(); | |
c.user = mysql_user; | |
c.password = mysql_pass; | |
c.database = database; | |
c.host = host, | |
c.port = port, | |
//c.connect(); | |
callback(null, c); | |
}, | |
destroy : function(client) {client.end();}, | |
max : 10, | |
min : 2, | |
idleTimeoutMillis : 30000, | |
log : true | |
}); | |
pool.acquire(function (err, client) { | |
client.query('select name from Label', [], function(error, _rows, _cols){ | |
pool.release(client); | |
if(error) { | |
console.log("ERROR : " + error); | |
return; | |
} | |
for(var i = 0; i < _rows.length; ++i) { | |
console.log("data : " + _rows[i].name); | |
} | |
}); | |
}); |