-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
9 changed files
with
568 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express'); | ||
var routes = require('./routes'); | ||
var user = require('./routes/user'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
var mysql = require('mysql'); | ||
var connection = mysql.createConnection({ | ||
host :'localhost', | ||
port : 3306, | ||
user : 'terry', | ||
password : 'asdf1234', | ||
database:'terry' | ||
}); | ||
connection.connect(function(err) { | ||
if (err) { | ||
console.error('mysql connection error'); | ||
console.error(err); | ||
throw err; | ||
} | ||
}); | ||
|
||
var app = express(); | ||
|
||
// all environments | ||
app.set('port', process.env.PORT || 3000); | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.favicon()); | ||
app.use(express.logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded()); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser('your secret here')); | ||
app.use(express.session()); | ||
app.use(app.router); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// development only | ||
if ('development' == app.get('env')) { | ||
app.use(express.errorHandler()); | ||
} | ||
|
||
// insert | ||
app.post('/users',function(req,res){ | ||
var user = {'userid':req.body.userid, | ||
'name':req.body.name, | ||
'address':req.body.address}; | ||
var query = connection.query('insert into users set ?',user,function(err,result){ | ||
if (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
console.log(query); | ||
res.send(200,'success'); | ||
}); | ||
}); | ||
|
||
// insert with transaction | ||
app.post('/userstx',function(req,res){ | ||
var user = {'userid':req.body.userid, | ||
'name':req.body.name, | ||
'address':req.body.address}; | ||
|
||
connection.beginTransaction(function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
connection.query('insert into users set ?', user, function (err, result) { | ||
if (err) { | ||
console.error(err); | ||
connection.rollback(function () { | ||
console.error('rollback error'); | ||
throw err; | ||
}); | ||
}// if err | ||
console.log('insert transaction log'); | ||
var log = {'userid': req.body.userid}; | ||
connection.query('insert into log set ?', log, function (err, result) { | ||
if (err) { | ||
console.error(err); | ||
connection.rollback(function () { | ||
console.error('rollback error'); | ||
throw err; | ||
}); | ||
}// if err | ||
connection.commit(function (err) { | ||
if (err) { | ||
console.error(err); | ||
connection.rollback(function () { | ||
console.error('rollback error'); | ||
throw err; | ||
}); | ||
}// if err | ||
res.send(200, 'success'); | ||
|
||
});// commit | ||
});// insert into log | ||
});// inset into users | ||
}); // begin trnsaction | ||
}); | ||
|
||
//select all | ||
app.get('/users', function(req,res){ | ||
var query = connection.query('select * from users',function(err,rows){ | ||
console.log(rows); | ||
res.json(rows); | ||
}); | ||
console.log(query); | ||
}); | ||
|
||
// select where | ||
app.get('/users/:userid', function(req,res){ | ||
var query = connection.query('select * from users where userid='+mysql.escape(req.params.userid),function(err,rows){ | ||
console.log(rows); | ||
res.json(rows); | ||
}); | ||
console.log(query); | ||
}); | ||
|
||
|
||
http.createServer(app).listen(app.get('port'), function(){ | ||
console.log('Express server listening on port ' + app.get('port')); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express'); | ||
var routes = require('./routes'); | ||
var user = require('./routes/user'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
// define validator | ||
function NameAlphabeticValidator(val){ | ||
return val.match("^[a-zA-Z\(\)]+$"); | ||
} | ||
function MemoLengthValidator(val){ | ||
if(val.length>10) return null; | ||
return val; | ||
} | ||
|
||
// schema definition with validation | ||
var MemoSchema= mongoose.Schema({ | ||
username:{type:String,validate:NameAlphabeticValidator} | ||
,memo:{type:String,validate:[ | ||
{validator:MemoLengthValidator,msg:'memo length should be less than 10'}, | ||
{validator:NameAlphabeticValidator,msg:'PATH `{PATH}` should be alphabet only. Current value is `{VALUE}` '} | ||
]} | ||
}); | ||
var Memo = mongoose.model('MemoModel',MemoSchema); // MemoModel : mongodb collection name | ||
|
||
var app = express(); | ||
|
||
// all environments | ||
app.set('port', process.env.PORT || 3000); | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded()); | ||
app.use(express.methodOverride()); | ||
app.use(app.router); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
app.post('/insert', function(req,res,err){ | ||
var memo = new Memo({username:req.body.username,memo:req.body.memo}); | ||
memo.save(function(err,silence){ | ||
if(err){ | ||
console.error(err); | ||
res.send(500,'error'); | ||
} | ||
res.send('success'); | ||
}); | ||
}); | ||
|
||
app.get('/users/:username', function(req,res,err){ | ||
var memos = new Memo(); | ||
Memo.findOne({'username':req.params.username},function(err,memo){ | ||
if(err){ | ||
console.err(err); | ||
throw err; | ||
} | ||
console.log(memo); | ||
res.send(200,memo); | ||
}); | ||
}); | ||
app.get('/users', function(req,res,err){ | ||
var memos = new Memo(); | ||
Memo.find().select('username').exec(function(err,memos){ | ||
if(err){ | ||
console.err(err); | ||
throw err; | ||
} | ||
console.log(memos); | ||
res.send(memos); | ||
}); | ||
}); | ||
|
||
mongoose.connect('mongodb://localhost/terrydb',function(err){ | ||
if(err){ | ||
console.log('mongoose connection error :'+err); | ||
throw err; | ||
} | ||
http.createServer(app).listen(app.get('port'), function(){ | ||
console.log('Express server listening on port ' + app.get('port')); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express'); | ||
var routes = require('./routes'); | ||
var http = require('http'); | ||
var path = require('path'); | ||
|
||
var app = express(); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
var httpServer =http.createServer(app).listen(8080, function(req,res){ | ||
console.log('Socket IO server has been started'); | ||
}); | ||
// upgrade http server to socket.io server | ||
var io = require('socket.io').listen(httpServer); | ||
|
||
var socket_ids = new Array(); | ||
var nicknames = new Array(); | ||
var count = 0; | ||
|
||
function registerUser(socket,nickname){ | ||
// 중복 제거 | ||
var previous_nickname = nicknames[socket.id]; | ||
console.log('previous nick:'+previous_nickname); | ||
if(previous_nickname != undefined){ | ||
delete socket_ids[previous_nickname]; | ||
} | ||
|
||
// socket_id와 nickname 테이블을 셋업 | ||
socket_ids[nickname] = socket.id | ||
nicknames[socket.id] = nickname; | ||
|
||
socket.broadcast.emit('userlist',{users:Object.keys(socket_ids)}); | ||
socket.emit('userlist',{users:Object.keys(socket_ids)}); | ||
} | ||
|
||
io.sockets.on('connection',function(socket){ | ||
socket.emit('new',{nickname:'GUEST-'+count}); | ||
registerUser(socket,'GUEST-'+count); | ||
count++; | ||
|
||
socket.on('changename',function(data){ | ||
console.log('change name socket '+socket.id+' '+data.nickname); | ||
registerUser(socket,data.nickname); | ||
console.log('nicks :'+Object.keys(socket_ids)); | ||
}); | ||
socket.on('disconnect',function(data){ | ||
nickname = nicknames[socket.id]; | ||
console.log('nickname '+nickname+' has been disconnected'); | ||
// 여기에 방을 나갔다는 메세지를 broad cast 하기 | ||
if(nickname != undefined){ | ||
delete socket_ids[nickname]; | ||
delete nicknames[socket.id]; | ||
}// if | ||
}); | ||
socket.on('send_msg',function(data){ | ||
data.msg = nicknames[socket.id] + ' : '+data.msg; | ||
if(data.to =='ALL') socket.broadcast.emit('broadcast_msg',data); // 자신을 제외하고 다른 클라이언트에게 보냄 | ||
else{ | ||
socket_id = socket_ids[data.to]; | ||
if(socket_id != undefined){ | ||
io.sockets.socket(socket_id).emit('broadcast_msg',data); | ||
}// if | ||
} | ||
socket.emit('broadcast_msg',data); // 해당 클라이언트에게만 보냄. 다른 클라이언트에 보낼려면? | ||
console.log('Message from client :'+data.msg); | ||
}) | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<html> | ||
<head> | ||
|
||
<title></title> | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> | ||
|
||
</head> | ||
<body> | ||
<b>Send message</b><p> | ||
Name <input type="text" id="nickname" /> <input type="button" id="changename" value="Change name"/><br> | ||
To | ||
<select id="to"> | ||
<option value="ALL">ALL</option> | ||
</select> | ||
Message <input type="text" id="msgbox"/> | ||
<br> | ||
<span id="msgs"></span> | ||
|
||
<script type="text/javascript"> | ||
var socket = io.connect('http://localhost'); | ||
$('#changename').click(function(){ | ||
socket.emit('changename',{nickname:$('#nickname').val()}); | ||
}); | ||
$("#msgbox").keyup(function(event) { | ||
if (event.which == 13) { | ||
socket.emit('send_msg',{to:$('#to').val(),msg:$('#msgbox').val()}); | ||
$('#msgbox').val(''); | ||
} | ||
}); | ||
socket.on('new',function(data){ | ||
console.log(data.nickname); | ||
$('#nickname').val(data.nickname); | ||
}); | ||
|
||
// 새로운 사용자가 들어오거나, 사용자가 이름을 바꿨을때 "To" 리스트를 변경함 | ||
socket.on('userlist',function(data){ | ||
var users = data.users; | ||
console.log(users); | ||
console.log(data.users.length); | ||
$('#to').empty().append('<option value="ALL">ALL</option>'); | ||
for(var i=0;i<data.users.length;i++){ | ||
$('#to').append('<option value="'+users[i]+'">'+users[i]+"</option>"); | ||
} | ||
}); | ||
|
||
socket.on('broadcast_msg',function(data){ | ||
console.log(data.msg); | ||
$('#msgs').append(data.msg+'<BR>'); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.