【问题标题】:NodeJS strings from client messages来自客户端消息的 NodeJS 字符串
【发布时间】:2011-06-09 18:40:07
【问题描述】:

如何从 NodeJS 中的消息中提取字符串?具体来说,我正在修改一个简单的聊天室示例以接受来自客户端的特定命令。

例子:

sock.on('connection', function(client){
    var s = the string in client.message...
    if(s == "specific string"){
        //do this
    }
    else{
        //do that
    }
});

我是 NodeJS 的新手,到目前为止,文档一直很有帮助。如果我以错误的方式处理这一切,我肯定会欣赏其他解决方案。谢谢。

编辑 1:服务器初始化

serv = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    // read index.html and send it to the client
    var output = fs.readFileSync('./index.html', 'utf8');
    res.end(output);
});
// run on port 8080
serv.listen(8080);

编辑 3:我意识到我不够具体,抱歉。这是一个显示我正在关注的教程的链接:http://spechal.com/2011/03/19/super-simple-node-js-chatroom/

具体来说,我想创建教程中提供的聊天室(我已经能够做到),然后检查人们相互广播的消息,看看它们是否包含特定的字符串。

例如,如果聊天室中的客户端提交了字符串“alpha”(输入 alpha,按下回车键),该字符串将被广播给所有其他客户端,服务器将通过广播字符串“Alpha has been received”来响应。”也给所有的客户。我的确切问题(据我所知)是我无法与事件侦听器收到的消息进行任何类型的字符串比较。是否可以从我的聊天室成员的消息中提取他们输入的文本?

【问题讨论】:

    标签: string node.js client-server message


    【解决方案1】:

    您的 'sock.on('data', function(data) {})' 处理程序在哪里?我认为 HTTP 示例实际上就是您正在寻找的,如下所列。 p>

    示例(用于 TCP 服务器):

    var server = net.Server(function(socket) {
      socket.setEncoding('ascii');
    
      socket.on('data', function(data) {
        // do something with data
      });
    
      socket.on('end', function() {
        // socket disconnected, cleanup
      });
    
      socket.on('error', function(exception) {
        // do something with exception
      });
    });
    server.listen(4000);
    

    HTTP 服务器示例:

    var http = require('http');
    var url = require('url');
    var fs = require('fs');
    
    var server = http.createServer(function (req, res) {
    
      // I am assuming you will be processing a GET request
      // in this example. Otherwise, a POST request would
      // require more work since you'd have to look at the
      // request body data.
    
      // Parse the URL, specifically looking at the
      // query string for a parameter called 'cmd'
      // Example: '/chat?cmd=index'
      var url_args = url.parse(req.url, true);
    
      // Should have error checking here...
      var cmd = url_args.query.cmd;
    
      res.writeHead(200, {'Content-Type': 'text/html'});
    
      var output;
      if (cmd == "index") {
        // read index.html and send it to the client
        output = fs.readFileSync('./index.html', 'utf8');
      } else if (cmd.length > 0) {
        output = "cmd was not recognized.";
      } else {
        output = "cmd was not specified in the query string.";
      }
      res.end(output);
    });
    
    server.listen(8080);
    

    【讨论】:

    • 我很抱歉我的无知,但我不太确定你问我什么。我会在编辑中发布我相信您要求我提出的内容。
    • 哦,我想我明白你现在的要求了。是的,我的处理程序函数位于我的服务器初始化代码之外和之后。它就在 serv.listen() 行之后。
    • 是的,文档有时会有点混乱。希望 HTTP 示例确实是您正在寻找的。 Node.JS 网站上的 Chat 示例非常复杂,但源代码似乎可读。他们在该示例中使用 HTTP 服务器。 github.com/ry/node_chat
    • 这是信息,但不是我想要的。我意识到我需要更具体一些,对此我深表歉意。
    • 你链接的github其实真的很酷。我觉得解决方案是在应用程序的服务器/客户端 .js 文件中提供的。我将此标记为答案,并建议查看此页面的人也检查该应用程序。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-05-16
    • 2015-06-04
    • 2017-06-18
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多