Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require("express");
- var app = express();
- var server = require("http").createServer(handler);
- var io = require("socket.io").listen(server);
- var fs = require('fs'),
- port = process.env.PORT || 3000,
- connectionsArray = [],
- mysql = require("mysql");
- var path = require('path');
- app.use(express.static(__dirname));
- server.listen("3000");
- /*
- var app = require('http').createServer(handler),
- io = require('socket.io').listen(app),
- fs = require('fs'),
- mysql = require('mysql'),
- connectionsArray = [], */
- var connection = mysql.createConnection({
- host: 'localhost',
- user: 'root',
- password: 'newpwd', //put your own mysql pwd
- database: 'test', //put your database name
- });
- var POLLING_INTERVAL = 1000;
- var pollingTimer;
- // If there is an error connecting to the database
- connection.connect(function(err) {
- // connected! (unless `err` is set)
- console.log(err);
- });
- // creating the server ( localhost:8000 )
- //server.listen(8000);
- // on server started we can load our client.html page
- function handler(req, res) {
- fs.readFile(__dirname + '/send.html', function(err, data) {
- if (err) {
- console.log(err);
- res.writeHead(500);
- return res.end('Error loading client.html');
- }
- res.writeHead(200);
- res.end(data);
- });
- }
- /*
- * HERE IT IS THE COOL PART
- * This function loops on itself since there are sockets connected
- * to the page. Upon Update it only emits the notification if
- * the value has changed.
- * Polling the database after a constant interval
- */
- var pollingLoop = function() {
- connection.query('SELECT * FROM activity_log', function(err, rows, fields) {
- pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
- updateSockets({
- users: rows
- });
- });
- };
- // creating a new websocket to keep the content updated without any AJAX request
- io.sockets.on('connection', function(socket) {
- //This variable is passed via the client at the time of socket //connection, see "io.connect(..." line in client.html
- profile_id = socket.handshake.query.profile_id;
- console.log('Number of connections:' + connectionsArray.length);
- // starting the loop only if at least there is one user connected
- if (!connectionsArray.length) {
- pollingLoop();
- }
- console.log('A new socket is connected!');
- connectionsArray.push(socket);
- socket.on('status', function(data) {
- //console.log(data);
- var values = data.split('-');
- var val = values[0];
- var vid = values[1];
- connection.query("UPDATE `activity_log` SET status = '" + val + "' WHERE id = '" + vid + "'", function(err, rows) {
- //connection.release();
- if (!err) {
- //callback(true);
- console.log('success');
- }
- });
- });
- });
- var updateSockets = function(data) {
- // sending new data to all the sockets connected
- connectionsArray.forEach(function(tmpSocket) {
- tmpSocket.volatile.emit('notification', data);
- });
- };
Add Comment
Please, Sign In to add comment