简体   繁体   English

使用Telnet的Node.js客户端和服务器游戏

[英]Node.js client and server game using Telnet

I'm trying to create a basic game (only text) using Node.js, and it's 'net' library. 我正在尝试使用Node.js创建一个基本游戏(只有文本),它是'net'库。
I'm hitting a bit of a wall. 我打了一堵墙。 I can't seem to figure out how to prompt the user to enter some information, and wait for the user to enter said information. 我似乎无法弄清楚如何提示用户输入一些信息,并等待用户输入所述信息。

Here's the code I have so far. 这是我到目前为止的代码。 It's fairly basic, and will print out 2 lines for you when you run the client, and then hang. 这是相当基本的,当你运行客户端时会打印出2行,然后挂起。 It's at that point I want the user to be able to enter information. 就在那时,我希望用户能够输入信息。 I'm unsure of a few things here: 我不确定这里的一些事情:
1. How do I enable the user to type? 1.如何让用户输入? (more specifically, is it client or server side) (更具体地说,是客户端还是服务器端)
2. How do I send that data to the server when enter is pressed? 2.按Enter键时如何将数据发送到服务器?

I have read up on some documentation as far as Telnet goes, and it leads me to my last question: is the Telnet module in Node really the right one for this, or is there a better alternative for client/server creation and communication? 我已经阅读了一些关于Telnet的文档,它引出了我的最后一个问题:Node中的Telnet模块是否真的是正确的,或者是否有更好的客户端/服务器创建和通信替代方案?



Client Code: 客户代码:

var connect = require('net');

var client = connect.connect('80', 'localhost');
console.log('Connection Success!\n\n');
client.on('data', function(data) {
  // Log the response from the HTTP server.
  console.log('' + data);
}).on('connect', function() {
  // Manually write an HTTP request.
  //I'm assuming I could send data at this point here, on connect?
}).on('end', function() {
  console.log('Disconnected');
});



Server Code: 服务器代码:

var net = require('net');

var sockets = [];

function cleanInput(data) {
    return data.toString().replace(/(\r\n|\n|\r)/gm,"");
}

function receiveData(socket, data) {
    var cleanData = cleanInput(data);
    if(cleanData === "quit") {
        socket.end('Goodbye!\n');
    }
    else {
        for(var i = 0; i<sockets.length; i++) {
            if (sockets[i] !== socket) {
                sockets[i].write(data);
            }
        }
    }
}

function closeSocket(socket) {
    var i = sockets.indexOf(socket);
    if (i != -1) {
        sockets.splice(i, 1);
    }
}

function newSocket(socket) {
    sockets.push(socket);
    socket.write('Welcome to the Battleship Server!\n\n');
    socket.write('Please enter a username: ');
    socket.on('data', function(data) {
        receiveData(socket, data);
    })
    socket.on('end', function() {
        closeSocket(socket);
    })
}


var server = net.createServer(newSocket);
server.listen(80);


Thanks in advance! 提前致谢!

  • you want to use process.stdin and process.stdout to interact with input/output from/to the terminal. 您希望使用process.stdinprocess.stdout与来自/到终端的输入/输出进行交互。 Have a look here . 看看这里
  • You can send data to the server when enter is pressed with process.stdin.once('data', function(data){ /* ... */ }) . 使用process.stdin.once('data', function(data){ /* ... */ })按下enter时,可以将数据发送到服务器。 The .once method ensures that the callback is called just once when the user hits enter. .once方法确保在用户点击进入时只调用一次回调。

The client code should look like: 客户端代码应如下所示:

var connect = require('net');

var client = connect.connect('80', 'localhost');
client.on('data', function(data) {
  console.log('' + data);
  process.stdin.once('data', function (chunk) {
    client.write(chunk.toString());
  });
}).on('connect', function() {
  client.write('Hello');
}).on('end', function() {
  console.log('Disconnected');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM