1

I am trying to include a module i found that will help manage users: http://www.codeproject.com/Articles/382561/Session-Management-in-Nodejs

Ive copied the code and put it in the same directory as my server.js

I require it by doing:

var express = require('express');
var http = require('http'), 
mysql = require("mysql");
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var sessionMgm = require("./sessionManagement");

Now in my socket i do this:

io.sockets.on('connection', function (socket) {

    socket.on('setUserInfo', function (data) {
        var sess = new Object();
        sess.sessionId = socket.id;
        sess.userId = data.userId;
        sess.username = data.username;
        sess.role = data.role;
        sessionMgm.add(sess);
    });


    socket.on("private", function(data) { 
        if(data.agentName.length <= 0) {
            data.agentName = 'Besökare';
        }       
        io.sockets.in('Room_' + data.user_id).emit('updatechat', data.agentName, data.msg);
        var user = sessionMgm.getSessionByUserId(data.id);

        console.log('::: A socket with ID ' + user + ' connected! ::: ');
    });

});

However i keep getting this error:

TypeError: Object # has no method 'getSessionByUserId'

Cant seem to figure out whats wrong, any ideas?

sessionManagement.js:

module.exports = sessionManagement;

var sessions = [];

//User roles list
var userRoles = {
  Admin: "administrator",
  User: "user",
  Supervisor: "supervisor"
};

var sessionManagement = {
  indexOf: function(sessionId) {
    for(var i in sessions) {
        if(sessions[i].sessionId == sessionId)
            return i;
    }

    return null;
  },
  indexOfUser: function(userId) {
    for(var i in sessions) {
        if(sessions[i].userId == userId)
            return i;
    }

    return null;
  },

  add: function(sessionData) {
    sessions.push(sessionData);
  },
  remove: function(sessionId) {
    var index = this.indexOf(sessionId);
    if(index != null) {
        sessions.splice(index, 1);
    } else {
        return null;
    }
  },
  removeByUserId: function(userId) {
    var index = this.indexOf(userId);
    if(index != null) {
        sessions.splice(index, 1);
    } else {
        return null;
    }
  },

  getSessionById: function(userId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        return sessions[index];
    } else {
        return null;
    }
  },
  getSessionByUserId: function(sessionId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        return sessions[index];
    } else {
        return null;
    }
  },

  isAdmin: function(userId) {
    var index = this.indexOfUser(userId);
    if(index != null) {
        if(users[index].role == userRoles.Admin) {
            return true;
        } else {
            return false;
        }
    } else {
        return null;
    }
  },
  getUsersByRole: function(role) {
    var usersByRole = [];
    for(var i in users) {
        if(users[i].role == role)
            usersByRole.push(users[i]);
    }

    return usersByRole;
  }
};

Yes, you can create it using canvas. But again, It will be only for html5 supported browsers and you can not achieve this using javascript in the browsers which dont support HTML5.

4

1 に答える 1

4

madflowが述べたように、あなたmodule.exports = sessionManagementsessionManagement.js

次に、エクスポートしていたためsessionManagement、初期化する前にエラーが発生しました。エクスポート行を最後に移動すると、sessionManagement.jsそれが修正されるはずです。

module.exports = sessionManagement; // <- you export here
    ...
    ...
    ...
var sessionManagement = { // and initialize here

sessionManagement宣言はモジュールの先頭に引き上げられますが(そのため、取得しない、またはUnexpected identifierReferenceError割り当てる場合module.exports)、初期化は行わないため、舞台裏で実際に行われるのは次のようなものです。

var sessionManagement;  // undefined at this point

module.exports = sessionManagement; // <- you export here, 
// but sessionManagement is undefined at this point 
// and so will be module.exports after this line
    ...
    ...
    ...
sessionManagement = { // and initialize here
于 2013-01-15T14:32:33.293 に答える