【问题标题】:Simple Chat application with NodeJS [duplicate]使用 NodeJS 的简单聊天应用程序 [重复]
【发布时间】:2018-03-28 14:52:37
【问题描述】:

我是 NodeJS 的新手,通过构建一个简单的命令行聊天应用程序开始学习。我有以下服务器和客户端代码。客户端-服务器通信成功,但我无法从客户端捕获“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);
});

【问题讨论】:

标签: node.js sockets client-server chat


【解决方案1】:

我猜你不必从客户端做:

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

});

所以你在 "user" 里面有一个对象 "users" 连接时,将 user 推送到数组 users 但您需要在(服务器端) on('close', ... 以在连接为 false 时从用户中删除用户 ... 等

【讨论】:

  • 感谢您对此的见解。我做了同样的事情,但是客户端中发出的“adduser”事件没有在服务器中捕获来为连接的客户端命名。
  • @Prashaanth 我在考虑 socket.io,我不知道这个网络是否类似,如果 adduser 没有运行,请尝试这个,原因是:不是 socket.io 和 lib NET无法添加新的事件发射器,解决方案从数据切换并添加具有多条件的对象来捕获您的数据。
  • 这可能会给我们答案。 stackoverflow.com/questions/6933646/…
  • 我猜如果你不能通过使用新的发射器服务器客户端来获取数据,那么你只有一个解决方案可以通过使用 on('data', ... 这个解决方案使用 telnet , socket.io 使用 real websocket TCP 和 api 直接从 socket API 获取更多信息。最后切换 socket.io 非常简单,你可以添加你想要的因为新的发射器和 mutch 示例可以为您提供快速的结果。
  • 我必须删除这个答案 ;) gl for next.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 2020-01-18
  • 2015-06-17
  • 2015-05-04
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多