2017-10-17 185 views
1

我是NodeJS的新手,並開始通過構建簡單的命令行聊天應用程序來學習。我有以下代碼服務器和客戶端。客戶端 - 服務器通信成功,但我無法從客戶端捕獲'adduser'事件。請告訴我我哪裏錯了。簡單的聊天應用程序與NodeJS

服務器:

var net = require('net'); 

var chatServer = net.createServer(function(socket){ 
    socket.pipe(socket); 
}), 
    userName=""; 

chatServer.on('connection',function(client){ 
    console.log("ChatterBox Server\n"); 
    client.write("Welcome to ChatterBox!\n"); 
    client.on('data',function(data){ 
     console.log(""+data); 
    }); 
    client.on('adduser',function(n){ 
     console.log("UserName: "+ n); 
     userName = n; 
    }); 
}); 

chatServer.listen(2708); 

客戶:

var net = require('net'); 
var client = new net.Socket(); 
client.connect(2708,'127.0.0.1'); 
client.on('connect',function(){ 
    client.emit('adduser',"UserName"); 
}); 
console.log("Client Connected!\n"); 
client.on('data',function(data){ 
    console.log(""+data); 
}); 
+0

我認爲這回答你的問題:https://stackoverflow.com/questions/6933646/socket-emit-in-a-simple-tcp-server-written-in-nodejs –

回答

0

我想你沒有從客戶端做:

client.connect(2708,'127.0.0.1'); 

只寫你的客戶喜歡這足夠。

var net = require('net'); 
var client = new net.Socket(); 

client.connect(2708, '127.0.0.1',function(){ 
    console.log("Client Connected!\n"); 
    client.emit('adduser',"UserName"); 
}); 

client.on('data',function(data){ 
    console.log(""+data); 
}); 

client.on('close', function() { 
    console.log('Connection closed'); 
}); 

所以服務器端:

var net = require('net'); 
var sockets = []; 
var port = 2708; 
var guestId = 0; 

var server = net.createServer(function(socket) { 
    // Increment 
    guestId++; 

    socket.nickname = "Guest" + guestId; 
    var userName = socket.nickname; 

    sockets.push(socket); 

    // Log it to the server output 
    console.log(userName + ' joined this chat.'); 

    // Welcome user to the socket 
    socket.write("Welcome to telnet chat!\n"); 

    // Broadcast to others excluding this socket 
    broadcast(userName, userName + ' joined this chat.\n'); 

    socket.on('adduser',function(n){ 
     console.log("UserName: "+ n); 
     userName = n; 
    }); 


    // When client sends data 
    socket.on('data', function(data) { 

     var message = clientName + '> ' + data.toString(); 

     broadcast(clientName, message); 

     // Log it to the server output 
     process.stdout.write(message); 
    }); 


    // When client leaves 
    socket.on('end', function() { 

     var message = clientName + ' left this chat\n'; 

     // Log it to the server output 
     process.stdout.write(message); 

     // Remove client from socket array 
     removeSocket(socket); 

     // Notify all clients 
     broadcast(clientName, message); 
    }); 


    // When socket gets errors 
    socket.on('error', function(error) { 

     console.log('Socket got problems: ', error.message); 

    }); 
}); 


// Broadcast to others, excluding the sender 
function broadcast(from, message) { 

    // If there are no sockets, then don't broadcast any messages 
    if (sockets.length === 0) { 
     process.stdout.write('Everyone left the chat'); 
     return; 
    } 

    // If there are clients remaining then broadcast message 
    sockets.forEach(function(socket, index, array){ 
     // Dont send any messages to the sender 
     if(socket.nickname === from) return; 

     socket.write(message); 

    }); 

}; 

// Remove disconnected client from sockets array 
function removeSocket(socket) { 

    sockets.splice(sockets.indexOf(socket), 1); 

}; 


// Listening for any problems with the server 
server.on('error', function(error) { 

    console.log("So we got problems!", error.message); 

}); 

// Listen for a port to telnet to 
// then in the terminal just run 'telnet localhost [port]' 
server.listen(port, function() { 

    console.log("Server listening at http://localhost:" + port); 

}); 

所以,你有一個對象「用戶」「用戶」當連接內,推用戶到陣列用戶但你需要做(服務器端)('關閉',...從用戶刪除用戶時,連接是錯誤的...等

+0

感謝您對此的見解。我做了同樣的事情,但在服務器中未捕獲客戶端發出的「adduser」事件以爲連接的客戶端指定名稱。 – Prashaanth

+0

@Prashaanth我正在考慮socket.io,我不知道如果這個網絡工作類似,嘗試這一個,如果adduser沒有運行,所以原因是:不是socket.io和lib NET不能添加新的事件發射器,該解決方案從數據切換並添加一個具有多個條件的對象來捕獲您的數據。 – 2017-10-17 07:25:19

+0

這可能會給我們答案。 https://stackoverflow.com/questions/6933646/socket-emit-in-a-simple-tcp-server-written-in-nodejs – Prashaanth