var express = require('express');
var http = require('http');
var app = express();
var socketio = require('socket.io');
var passportSocketIo = require('passport.socketio');
var server = http.createServer(app);
var io = socketio.listen(server)
var morgan = require('morgan');
var path = require('path');
var session = require('express-session');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')('thingstream');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var SQLiteStore = require('connect-sqlite3')(session);
var favicon = require('serve-favicon');
mongoose.connect(URL);
// some environment variables
app.set('views', dirname + '/views');
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(favicon(path.join(dirname, 'public', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser);
// parse request bodies (req.body)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({
extended: true
}));
// session support
var sessionStore = new SQLiteStore({
db: 'sessions',
dir: 'C:/Users/B.X/Desktop/Dev_Playground/sqlite'
});
var sharedSessions = session({
key: 'express.sid',
store: sessionStore,
resave: false, // don't save session if unmodified
saveUninitialized: true,
secret: 'thingstream',
});
app.use(sharedSessions);
// pass passport for configuration
require('./middleware/passport')(passport);
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
io.use(passportSocketIo.authorize({
cookieParser: require('cookie-parser'),
key: 'express.sid',
secret: 'thingstream',
store: sessionStore,
passport: passport,
success: onAuthorizeSuccess, // optional callback on success
fail: onAuthorizeFail, // optional callback on fail/error
}));
function onAuthorizeSuccess(data, accept) {
console.log('successful connection to socket.io');
// The accept-callback still allows us to decide whether to
// accept the connection or not.
// accept connection
accept();
function onAuthorizeFail(data, message, error, accept) {
// error indicates whether the fail is due to an error or just a
unauthorized client
if (error) throw new Error(message);
//console.log(data);
console.log('failed connection to socket.io:', message);
// send the (not-fatal) error-message to the client and deny the
connection
return accept(new Error(message));
}
// routes ======================================================================
//==============================================================================
//starting server
server.listen(3000, function() {
console.log('server started on port 3000');
});`
after performing connection to the server with a standalone socketio client
I get "failed connection to socket.io: No session found" on my server console
here is my client:
`//client.js
var io = require('socket.io-client');
var socket = io.connect('http://127.0.0.1:3000', {
reconnect: true
});
I have passport.socketio configured properly on my server:
`//server.js const URL = 'mongodb://127.0.0.1/node'; const util = require('util');
var express = require('express'); var http = require('http'); var app = express(); var socketio = require('socket.io'); var passportSocketIo = require('passport.socketio'); var server = http.createServer(app); var io = socketio.listen(server) var morgan = require('morgan'); var path = require('path'); var session = require('express-session'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser')('thingstream');
var mongoose = require('mongoose'); var passport = require('passport'); var flash = require('connect-flash'); var SQLiteStore = require('connect-sqlite3')(session); var favicon = require('serve-favicon');
// configuration ===============================================================
mongoose.connect(URL); // some environment variables app.set('views', dirname + '/views'); // set the view engine to ejs app.set('view engine', 'ejs'); app.use(favicon(path.join(dirname, 'public', 'favicon.ico'))); app.use(express.static(path.join(__dirname, 'public'))); app.use(cookieParser); // parse request bodies (req.body) app.use(bodyParser.json()); // get information from html forms app.use(bodyParser.urlencoded({ extended: true }));
// session support var sessionStore = new SQLiteStore({ db: 'sessions', dir: 'C:/Users/B.X/Desktop/Dev_Playground/sqlite' });
var sharedSessions = session({ key: 'express.sid', store: sessionStore, resave: false, // don't save session if unmodified saveUninitialized: true, secret: 'thingstream', });
app.use(sharedSessions); // pass passport for configuration require('./middleware/passport')(passport); app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session
io.use(passportSocketIo.authorize({ cookieParser: require('cookie-parser'), key: 'express.sid', secret: 'thingstream', store: sessionStore, passport: passport, success: onAuthorizeSuccess, // optional callback on success fail: onAuthorizeFail, // optional callback on fail/error })); function onAuthorizeSuccess(data, accept) { console.log('successful connection to socket.io');
// The accept-callback still allows us to decide whether to // accept the connection or not. // accept connection accept();
// reject connection (for whatever reason) accept(new Error('optional reason')); }
function onAuthorizeFail(data, message, error, accept) { // error indicates whether the fail is due to an error or just a unauthorized client if (error) throw new Error(message); //console.log(data); console.log('failed connection to socket.io:', message); // send the (not-fatal) error-message to the client and deny the connection return accept(new Error(message)); } // routes ======================================================================
//============================================================================== io.on('connection', function(socket) { console.log('connection');
socket.on('CH01', function(from, msg) { console.log('MSG', from, ' saying ', msg); });
});
//============================================================================== //starting server server.listen(3000, function() { console.log('server started on port 3000'); });`
after performing connection to the server with a standalone socketio client I get "failed connection to socket.io: No session found" on my server console
here is my client:
`//client.js var io = require('socket.io-client'); var socket = io.connect('http://127.0.0.1:3000', { reconnect: true });
// Add a connect listener socket.on('connect', function(socket) { console.log('Connected!'); }); socket.emit('CH01', 'me', 'test msg');`
can't find where my problem is cause it makes sense after fetching the SQLite database which is empty.