Advertisement
Guest User

Untitled

a guest
Jan 24th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var port = 1337;
  2. var io = require("socket.io").listen( port );
  3. var colors = require('colors');
  4. var Sequelize = require("sequelize");
  5. var sequelize = new Sequelize('authentication', 'root', 'papatool2001', {
  6.     host: "localhost",
  7.     port: 3306,
  8.     dialect: 'mysql'
  9. });
  10.  
  11. var seqConnection = sequelize.authenticate()
  12.     .then(function () {
  13.         console.log("Connected successfully to the database! ".green);
  14.     })
  15.     .catch(function (err) {
  16.         console.log("Connected failed to the database".red);
  17.     })
  18.     .done();
  19.  
  20. var users = 0;
  21.  
  22. io.on("connection", function(socket) {
  23.     // sequelize
  24.     var User = sequelize.define('User', {
  25.     id      : Sequelize.INTEGER,
  26.     username: Sequelize.STRING,
  27.     password: Sequelize.STRING
  28.     }, {
  29.     tableName: 'accounts', // this will define the table's name
  30.     timestamps: false           // this will deactivate the timestamp columns
  31.     })
  32.     // sequelize end
  33.     users++;
  34.     console.log( 'Client verbunden');
  35.  
  36.     io.emit("userChange", users);
  37.  
  38.     socket.on("_ping", function() {
  39.         socket.emit("_pong");
  40.     });
  41.  
  42.     socket.on("disconnect", function() {
  43.         users--;
  44.         io.emit("userChange", users);
  45.     });
  46.  
  47.     socket.on("authentication", function(data){
  48.         var username = data.username;
  49.         var password = data.password;
  50.         User
  51.           .find({ where: { username: username } })
  52.           .then(function(err, username) {
  53.             if (!username) {
  54.               console.log('No user with the username has been found.');
  55.             } else {
  56.               console.log('Hello ' + username + '!');
  57.             }
  58.           });
  59.     });
  60.  
  61. });
  62.  
  63. console.log( "Server erfolgreich gestartet".green );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement