具有nodeJs

时间:2017-12-27 20:43:23

标签: javascript node.js login

我是nodeJs的新手,我找不到我的问题的解决方案。我有一个登录界面我不想在2个用户连接后显示警告。我的问题是警报始终显示第二个用户的联系。 如何在第二个用户登录后显示: 这是我的client.js代码:

 var socket = io.connect('http://localhost:5050');
 $('#loginform').submit(function(event){
        event.preventDefault();

        socket.emit('login' , {
            username : $('#username').val(),
            mail     : $('#mail').val()

        })
    });
    socket.on('loged' , function(){

       $('#login').fadeOut();

});

    socket.on('newuser' , function(user){
        $('#users').append('<img src="' + user.avatar + '">');
    })  

我的代码server.js

    var http = require('http');
var fs = require('fs');
var md5 = require('MD5');
var alert=require('alert-node');

var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
       });
});

var io = require('socket.io').listen(server);
var users = {};
var i=0;

io.sockets.on('connection', function (socket,user) {
    var me;
    console.log('new user is connected');


for (k in users) {

        socket.emit('newuser' , users[k]);
    }
socket.on('login' , function(user){

       me=user;
        me.id=user.mail.replace('@','-').replace('.','-');
        me.avatar = 'https://gravatar.com/avatar/' + md5(user.mail) + '?s=50';
        socket.emit('loged');
        users[me.id] = me;
         io.sockets.emit('newuser', me);

});

}); 

server.listen(5050);

1 个答案:

答案 0 :(得分:0)

我没有看到有关您的代码的任何提醒,因此我假设以下内容,您可以通过// Check if file was uploaded without errors if(isset($_FILES["school_logo"]) && $_FILES["school_logo"]["error"] == 0) { // allowed image types $allowed = array( "jpg" => "image/jpg", "pjpg" => "image/pjpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png" ); // some (incoming) file properties $filename = $_FILES["school_logo"]["name"]; $filetype = $_FILES["school_logo"]["type"]; $filesize = $_FILES["school_logo"]["size"]; // Verify file extension (here we are comparing the file extension to the keys of $allowed array) $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) { echo "Error: Please select a valid file format.<br/>"; } // Verify file size - 5MB maximum (5MB is example, you can set anything) $maxsize = 5 * 1024 * 1024; if($filesize > $maxsize) { echo "Error: File size is larger than the allowed limit.<br/>"; } // Verify MYME type of the file (here we are comparing the file MYME type to the $allowed array values) if(in_array($filetype, $allowed)) { $db_coulm_val = ""; $directory = "../uploads/schools_logos/"; $logo_name = $_FILES['school_logo']['name']; $twmporayr_logo_name = $_FILES['school_logo']['tmp_name']; $complete_directory_name = $directory.time().$_FILES['school_logo']['name']; // Check whether file exists before uploading it if(file_exists("../uploads/schools_logos/" . $_FILES["school_logo"]["name"])) { echo $_FILES["school_logo"]["name"] . " already exists.<br/>"; } else { move_uploaded_file($twmporayr_logo_name, $complete_directory_name); $db_coulm_val = " s_logo = '" . $logo_name . "', "; } } else { echo "Error: There was a problem uploading your file. Please try again.<br/>"; } } else { echo "Error: " . $_FILES["school_logo"]["error"] . "<br/>"; } 或已断开连接i++查看已连接的用户数量,但您不会在代码中的任何地方使用它,在这种情况下,我使用i--来存储用户。请注意,如果您在Node.js脚本中使用Map,只需使用alert-node,如果您在浏览器中添加console.log,那么它将使用alert

const http = require('http');
const fs = require('fs');
const md5 = require('MD5');
const alert=require('alert-node');

var server = http.createServer(function(req, res) {
    fs.readFile('./index.html', 'utf-8', function(error, content) {
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end(content);
   });
});

var io = require('socket.io').listen(server);
var users = new Map();

io.sockets.on('connection', function (socket,user) {
    console.log('new user is connected');
    for (k in users) {
        socket.emit('newuser' , users[k]);
    }
    socket.on('login' , function(user){
        let me = user;
        me.id=user.mail.replace('@','-').replace('.','-');
        me.avatar = 'https://gravatar.com/avatar/' + md5(me.mail) + '?s=50';
        users.set(me.id,me)
        io.sockets.emit('newuser', me);
        socket.emit('loged');
        if(users.size === 2){
            // notify that they are two users connected?
        }
    });

});

server.listen(5050);

就个人而言,我会创建一个扩展EventEmitter的类,并使用它更容易维护等等。