livescore server script
var pmx = require('pmx'); | |
var express = require('express'); | |
var pool = require('./db_connect.js'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var io = require('socket.io')(server); | |
var ejs = require('ejs'); | |
app.set('view engine', 'ejs'); | |
app.set('views', __dirname + '/templates'); | |
var probe = pmx.probe(); | |
var users_connected = probe.counter({ | |
name : 'users connected' | |
}); | |
var queries = probe.meter({ | |
name : 'queries/min', | |
samples : 60, | |
timeframe : 300 | |
}); | |
var emited_matches = probe.meter({ | |
name : 'emited matches/min', | |
samples : 60, | |
timeframe : 300 | |
}); | |
var emited_events = probe.meter({ | |
name : 'emited events/min', | |
samples : 60, | |
timeframe : 300 | |
}); | |
io.on('connection', function(socket){ | |
console.log('User connected'); | |
users_connected.inc(); | |
socket.on('match', function(data){ | |
socket.join('match_' + data.match_id); | |
console.log('client joined to room match_' + data.match_id); | |
}); | |
socket.on('disconnect', function(){ | |
console.log('User disconected'); | |
users_connected.dec(); | |
}); | |
}); | |
function emitMatch(match_id) { | |
pool.getConnection(function(err,connection) { | |
connection.query('SELECT * from Livescore_soccer_history_match WHERE match_id = ?', [connection.escape(match_id)], function (err, rows, fields) { | |
queries.mark(); | |
if (rows[0] !== null && typeof rows[0] !== "undefined") { | |
io.to('match_' + match_id).emit('match', rows[0]); | |
emited_matches.mark(); | |
console.log('emited match to room match_' + match_id); | |
} | |
connection.release(); | |
}); | |
}); | |
} | |
function emitEventsHtml(match_id) { | |
pool.getConnection(function(err,connection) { | |
connection.query('SELECT * from Livescore_soccer_history_match_event WHERE match_id = ? AND code != "SO" AND code != "AS" ORDER BY minute, minute_extra, event_id', [connection.escape(match_id)], function (err, rows, fields) { | |
queries.mark(); | |
var events = rows; | |
connection.query('SELECT * from Livescore_soccer_history_match WHERE match_id = ?', [connection.escape(match_id)], function (err, rows, fields) { | |
queries.mark(); | |
var match = rows[0]; | |
app.render('events', {'events': events, 'match': match}, function (err, html) { | |
if (html !== null && typeof html !== "undefined") { | |
io.to('match_' + match_id).emit('events_html', html); | |
emited_events.mark(); | |
console.log('emited events_html to room match_' + match_id); | |
} | |
if (err) { | |
console.log(err); | |
} | |
}); | |
connection.release(); | |
}); | |
}); | |
}); | |
} | |
function deleteMatch(match_id) { | |
pool.getConnection(function(err,connection) { | |
connection.query('DELETE FROM Livescore_soccer_node_match WHERE match_id = ?', [connection.escape(match_id)], function (err, rows, fields) { | |
queries.mark(); | |
connection.release(); | |
}); | |
}); | |
} | |
function getMatches() { | |
pool.getConnection(function(err,connection) { | |
connection.query('SELECT * from Livescore_soccer_node_match', function (err, matches, fields) { | |
queries.mark(); | |
for (var i in matches) { | |
if (matches[i].match) { | |
emitMatch(matches[i].match_id); | |
} | |
if (matches[i].event) { | |
emitEventsHtml(matches[i].match_id); | |
} | |
deleteMatch(matches[i].match_id); | |
} | |
if (err) { | |
console.log(err); | |
} | |
connection.release(); | |
}); | |
if (err) { | |
console.log(err); | |
} | |
}); | |
getMatchesTimeout(); | |
} | |
function getMatchesTimeout() { | |
setTimeout(function () { | |
getMatches(); | |
}, 1000); | |
} | |
getMatchesTimeout(); | |
server.listen(3000, function(){ | |
console.log('Listening on port 3000...'); | |
}); |
This comment has been minimized.
This comment has been minimized.
nice! I'm wondering what's in db_connect.js. Can you link to the full project? :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
How can i create livescore ?? Sir.