简单聊天应用程序与NodeJS

我是NodeJS的新手,开始学习构build一个简单的命令行聊天应用程序。 我有以下代码为服务器和客户端。 客户端 – 服务器通信成功,但我无法从客户端捕获“adduser”事件。 请告诉我哪里错了。

服务器:

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); }); 

我想你不需要从客户端做:

 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); }); 

所以当你连接的时候,你在用户里面有一个“用户”对象,把用户推到数组用户上,但是你需要在服务器端('close',…从用户中删除用户连接是错误的…等等