Advertisement
relax4o

nodejs

Jul 21st, 2013
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http').createServer(onRequest),
  2.     io = require('socket.io').listen(http),
  3.     mysql = require('mysql'),
  4.     fs = require('fs');
  5.    
  6. http.listen(8080);
  7.    
  8. var client = mysql.createConnection( {
  9.     host: 'localhost',
  10.     user: 'root',
  11.     password: ''
  12. } );
  13.  
  14. function onRequest( request, response ) {
  15.     console.log('Request received!');
  16.     response.writeHead( 200, { 'Content-Type': 'text/html' } );
  17.     response.end();
  18. }
  19.  
  20. io.sockets.on('connection', function ( socket ) {
  21.    
  22.     socket.on( 'actionbar', function ( char_id ) {
  23.         var table = 'character_action';
  24.         var database = 'n_characters';
  25.         var output = 0;
  26.        
  27.         client.query( 'USE ' + database );
  28.         client.query( 'SELECT data FROM ' + table + ' WHERE character_id = ' + char_id, function ( err, rows ) {
  29.             if ( err ) throw err;
  30.            
  31.             if ( rows[0]['data'].length != 0 )
  32.                 output = rows[0]['data'];
  33.             else
  34.                 output = 0;
  35.                
  36.             socket.emit( 'result', output );
  37.         } );
  38.        
  39.         socket.on( 'update', function ( data ) {
  40.             console.log('Update request received!');
  41.             client.query( 'UPDATE ' + table + ' SET data = \'' + data + '\' WHERE character_id = ' + char_id, function ( err, result ) {
  42.                 if ( err ) throw err;
  43.             } );
  44.         } );
  45.     });
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement