mirror of
https://github.com/sstent/node.git
synced 2026-01-25 14:42:00 +00:00
first post
This commit is contained in:
44
Nodejs-Socketio-Mysql-Demo/app.js
Normal file
44
Nodejs-Socketio-Mysql-Demo/app.js
Normal file
@@ -0,0 +1,44 @@
|
||||
var fs = require('fs');
|
||||
var db_helper = require("./db_helper.js");
|
||||
|
||||
var http = require('http').createServer(function handler(req, res) {
|
||||
fs.readFile(__dirname + '/index.html', function(err, data) {
|
||||
if (err) {
|
||||
res.writeHead(500);
|
||||
return res.end('Error loading index.html');
|
||||
} else {
|
||||
res.writeHead(200);
|
||||
res.end(data);
|
||||
}
|
||||
});
|
||||
}).listen(3000);
|
||||
|
||||
var io = require('socket.io').listen(http);
|
||||
io.sockets.on('connection', function(client) {
|
||||
console.log('Client connected');
|
||||
|
||||
// populate employees on client
|
||||
db_helper.get_employees(function(employees) {
|
||||
client.emit('populate', employees);
|
||||
});
|
||||
|
||||
// client add new employee
|
||||
client.on('add employee', function(data) {
|
||||
// create employee, when its done repopulate employees on client
|
||||
db_helper.add_employee(data, function(lastId) {
|
||||
// repopulate employees on client
|
||||
db_helper.get_employees(function(employees) {
|
||||
client.emit('populate', employees);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user