-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
95 lines (72 loc) · 2.2 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var mysql = require("mysql");
var express = require("express"),
app = express();
app.use('/pages', express.static(__dirname + "/pages"));
app.get('/',function(req, res){
res.sendfile(__dirname + '/pages/html/index.html');
});
app.get('/alarm',function(req, res){
res.sendfile(__dirname + '/pages/html/alarm.html');
});
app.get('/weight',function(req, res){
res.sendfile(__dirname + '/pages/html/weight.html');
});
var server = require("http").createServer(app).listen(8888, function(){
console.log("Server Running.....\nListening : localhost:8888");
});
var io = require("socket.io").listen(server);
var data_mysql = {
table: "Food_Water",
old_length: 0
};
var db = mysql.createConnection({
host: "localhost",
port: 3306,
user: "Phuckks_User",
password: "Phuckks_Pass",
dateStrings: true,
database: "Phuckks_DB"
});
/*---------------------------------------------------------------------------------*/
function mysql_connect(){
db.connect(function (err) {
if(err)
throw(err);
console.log("Connected MySQL");
});
}
function mysql_query(sql, callback){
db.query(sql, function(err, result, fields){
if(err)
throw err;
return callback(result);
});
}
function mysql_close(){
db.end();
console.log("Disconnected MySQL");
}
mysql_connect();
var j=0;
setInterval(function () {
// mysql_query("SELECT * FROM " + data_mysql.table+ " ORDER BY ID DESC LIMIT 20", function(callback){
// io.sockets.emit("Data_Food_Water", JSON.stringify(callback));
// console.log(JSON.stringify(callback));
// });
}, 1000);
io.sockets.on("connection", function (socket) {
console.log(socket.id + " connected");
socket.on("disconnect", function () {
console.log(socket.id + "disconnect");
});
socket.on("request_localeTime", function (data) {
var timeNow;
timeNow = new Date().toLocaleString('en-US', {timeZone: 'Asia/Ho_Chi_Minh'});
console.log(timeNow);
socket.emit("response_localeTime", timeNow);
});
socket.on("Click", function (data) {
io.sockets.emit("Click", JSON.stringify(data));
console.log("emit...ON");
});
});