0
I am developing basic application that has a simple membership. Besides, i need to develop Sessions aswell. In my following code, sockets do not respond. Can you help me to find the reason behind that ?
var WebApp = require('./webApp.js');
var db = require('./db/db.js');
var db = new db();
var webApp = new WebApp();
var App = function(){}
webApp.initialize();
webApp.socketIOConnect(function(client){
webApp.socketIOConnectMethods(client);
});
module.exports = App;
webApp.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var router = express.Router();
var path = require('path');
var Db = require('./db/db.js');
var connect = require('connect');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var socketHandshake = require('socket.io-handshake');
var io = require('socket.io')(server);
io.use(socketHandshake({secret:'secret',resave : false, saveUninitialized : true, parser:cookieParser()}));
server.listen(1185);
var WebApp = function() {
console.log("Greetings from WebApp Module.");
}
var db = new Db();
WebApp.prototype.initialize = function() {
app.use(express.static("./assets"));
router.get('/', function (req, res, next) {
res.sendFile("./assets/index.html");
});
router.get('/login', function (req, res, next) {
res.sendFile(path.resolve("./assets/login.html"));
});
router.get('/client', function (req, res, next) {
res.sendFile(path.resolve("./client/client.html"));
});
app.use(router);
}
WebApp.prototype.socketIOConnect = function(callback) {
io.on('connection', function(client, req, res) {
callback(client)
});
}
WebApp.prototype.socketIOConnectMethods = function (client) {
if (client.handshake.session.name) {
console.log('We already know you!');
client.emit('get name');
client.emit('redirect');
};
client.on('join', function(data) {
console.log(data);
});
client.on('register', function(data) {
client.emit('username', data);
console.log(data);
var checkAuth;
var username = data.user;
var password = data.passw;
var email = data.email;
var confpass = data.confirmPass;
console.log("password : "+password);
console.log("conf password :"+confpass);
if (password == confpass){
console.log("Passwords match, this lad can login");
var values = [[, username, password, email]];
console.log(username + " " + password + " " + email);
db.registAuth(email,function(err, results) {
if (err) {
console.log("An error occurred: " + err.message);
}
client.on('passwordmatches',function(data){
console.log(data);
});
console.log(results);
var checkAuth = results.length;
if(results < 1){
db.userRegistration(values,function(err, results) {
if(err) {
console.log("An error occurred: " + err.message);
}
console.log(results);
});//user registration
}
else{console.log("Sorry, we could not complete your registration. Email already exists.");}
});//registAuth
}
else{
client.on('NoMatchPass',function(data){
console.log(data);
});
console.log("Sorry, we could not complete your registration. Passwords do not match !");
client.emit('tryagainreg',"Sorry, we could not complete your registration. Passwords do not match !");
}
});//client.on register
client.on('login', function(data) {
var email = data.email;
var password = data.password;
console.log(data);
db.loginAuth(email, password, function(err, results) {
if (err) {
console.log("An error occurred: " + err.message);
}
console.log(results.length);
if (results.length == 1){
console.log("Welcome, "+email+"!");
client.handhsake.session.name = email;
client.handshake.session.save();
console.log(client.handshake.session.name);
client.emit('AuthLogin',email);
}
else{console.log("Wrong username or password.");
client.emit('FailedLogin',email);
}
});
});
};
module.exports = WebApp;
사용자가 성공적으로 로그인을 마친 후 성공적으로 로그인 한 후 client.html로 리디렉션하려고하지만 세션을 통해 응용 프로그램에서 로그 아웃 할 때까지 권한을 유지할 수있게하려고합니다.
단순히 아래에이 코드를 사용하여 내 문제를 해결
<!doctype html>
<html lang="en">
<head>
\t <meta charset="utf-8" />
\t <link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png">
\t <link rel="icon" type="image/png" href="../assets/img/favicon.png">
\t <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
\t <title>Let's Get Head In</title>
\t <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
\t <!-- Fonts and icons -->
\t <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" />
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" />
\t <!-- CSS Files -->
<link href="./css/bootstrap.min.css" rel="stylesheet" />
<link href="./css/material-kit.css" rel="stylesheet"/>
</head>
<body class="signup-page">
\t <nav class="navbar navbar-transparent navbar-absolute">
\t <div class="container">
\t <!-- Brand and toggle get grouped for better mobile display -->
\t <div class="navbar-header">
\t \t <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigation-example">
\t \t <span class="sr-only">Toggle navigation</span>
\t \t <span class="icon-bar"></span>
\t \t <span class="icon-bar"></span>
\t \t <span class="icon-bar"></span>
\t \t </button>
\t \t <a class="navbar-brand" href="">AslanServices</a>
\t </div>
\t <div class="collapse navbar-collapse" id="navigation-example">
\t \t <ul class="nav navbar-nav navbar-right">
\t \t \t \t \t <li>
\t \t \t \t \t <a href="./index.html" target="">
\t \t \t \t \t \t Register
\t \t \t \t \t </a>
\t \t \t \t </li>
\t \t \t \t <li>
\t \t \t \t \t \t <a href="" target="">
\t \t \t \t \t \t \t <i class="material-icons">unarchive</i>Contact
\t \t \t \t \t \t </a>
\t \t \t \t </li>
\t \t </ul>
\t </div>
\t </div>
</nav>
<div class="wrapper">
\t \t <div class="header header-filter" style="background-image: url('./img/city.jpg'); background-size: cover; background-position: top center;">
\t \t \t <div class="container">
\t \t \t \t <div class="row">
\t \t \t \t \t <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3">
\t \t \t \t \t \t <div class="card card-signup">
\t \t \t \t \t \t \t <form class="loginform" method="" action="">
\t \t \t \t \t \t \t \t <div class="header header-primary text-center">
\t \t \t \t \t \t \t \t \t <h4>Sign In</h4>
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t \t <div class="content">
\t \t \t \t \t \t \t \t \t <div class="input-group">
\t \t \t \t \t \t \t \t \t \t <span class="input-group-addon">
\t \t \t \t \t \t \t \t \t \t \t <i class="material-icons">email</i>
\t \t \t \t \t \t \t \t \t \t </span>
\t \t \t \t \t \t \t \t \t \t <input type="text" class="form-control" id="email" placeholder="Email...">
\t \t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t \t \t <div class="input-group">
\t \t \t \t \t \t \t \t \t \t <span class="input-group-addon">
\t \t \t \t \t \t \t \t \t \t \t <i class="material-icons">lock_outline</i>
\t \t \t \t \t \t \t \t \t \t </span>
\t \t \t \t \t \t \t \t \t \t <input type="password" placeholder="Password..." id="password" class="form-control" />
\t \t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t \t \t <!-- If you want to add a checkbox to this form, uncomment this code
\t \t \t \t \t \t \t \t \t <div class="checkbox">
\t \t \t \t \t \t \t \t \t \t <label>
\t \t \t \t \t \t \t \t \t \t \t <input type="checkbox" name="optionsCheckboxes" checked>
\t \t \t \t \t \t \t \t \t \t \t Subscribe to newsletter
\t \t \t \t \t \t \t \t \t \t </label>
\t \t \t \t \t \t \t \t \t </div> -->
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t \t <div class="footer text-center">
\t \t \t \t \t \t \t \t \t <button type="submit" class="btn" value="Submit">Sign in!</button>
\t \t \t \t \t \t \t \t </div>
\t \t \t \t \t \t \t </form>
\t \t \t \t \t \t </div>
\t \t \t \t \t </div>
\t \t \t \t </div>
\t \t \t </div>
\t \t \t <footer class="footer">
\t \t <div class="container">
\t \t <nav class="pull-left">
\t \t \t \t \t \t <ul>
\t \t \t \t \t \t \t <li>
\t \t \t \t \t \t \t \t <a href="https://www.instagram.com/ozercevikaslan/">
\t \t \t \t \t \t \t \t \t AslanServices
\t \t \t \t \t \t \t \t </a>
\t \t \t \t \t \t \t </li>
\t \t \t \t \t \t \t <li>
\t \t \t \t \t \t \t \t <a href="https://www.instagram.com/ozercevikaslan/">
\t \t \t \t \t \t \t \t About Us
\t \t \t \t \t \t \t \t </a>
\t \t \t \t \t \t \t </li>
\t \t \t \t \t \t </ul>
\t \t </nav>
\t \t <div class="copyright pull-right">
\t \t © 2017, made by <a href="https://www.instagram.com/ozercevikaslan/" target="_blank">Aslanmeister</a>
\t \t </div>
\t \t </div>
\t \t </footer>
\t \t </div>
</div>
</body>
\t <!-- Core JS Files -->
\t <script src="./js/jquery.min.js" type="text/javascript"></script>
\t <script src="./js/bootstrap.min.js" type="text/javascript"></script>
\t <script src="./js/material.min.js"></script>
\t <!-- Plugin for the Sliders, full documentation here: http://refreshless.com/nouislider/ -->
\t <script src="./js/nouislider.min.js" type="text/javascript"></script>
\t <!-- Plugin for the Datepicker, full documentation here: http://www.eyecon.ro/bootstrap-datepicker/ -->
\t <script src="./js/bootstrap-datepicker.js" type="text/javascript"></script>
\t <!-- Control Center for Material Kit: activating the ripples, parallax effects, scripts from the example pages etc -->
\t <script src="./js/material-kit.js" type="text/javascript"></script>
\t <script src="/socket.io/socket.io.js"></script>
\t <script>
var socket = io.connect('127.0.0.1:1185');
\t \t \t \t socket.on('connect', function(data) {
socket.emit('join', 'Hello World from client');
\t \t \t \t \t socket.on('redirect',function(callback){ \t \t callback(window.location.href="127.0.0.1:1185/client");
\t \t \t \t \t \t \t \t });
\t \t \t \t $('form.loginform').submit(function(event){
event.preventDefault();
var email = $('#email').val();
var password = $('#password').val();
socket.emit('login',{email : email, password : password});
socket.on('AuthLogin',function(data){window.location.href = 127.0.0.1:1185/client";});
socket.on('FailedLogin',function(data){alert('Wrong username or Password. Maybe, you dont even exist!');window.location.href = "127.0.0.1:1185";});
});
\t \t \t \t });
\t \t \t \t \t \t </script>
</html>