开发者

NodeJS strings from client messages

How can you extract strings from messages in NodeJS? Specifically I'm modifying a simple chat room example to accept specific commands from clients.

Example:

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

I'm new to NodeJS and the documentation has been very helpful up till now. If I'm approaching this all the wrong way I'd definitely appreciate alternative solutions. Thanks.

Edit 1: server initialization

serv = http.createServer(function(req, res){
    res.writeHead(2开发者_开发百科00, {'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);

Edit 3: I realize that I haven't been specific enough, sorry. Here's a link showing the tutorial I'm following: http://spechal.com/2011/03/19/super-simple-node-js-chatroom/.

Specifically I'd like to create the chatroom supplied in the tutorial (which I've been able to do), and then check the messages that people are broadcasting to each other to see if they contain specific strings.

For example, if a client in the chatroom submitted the string "alpha" (types alpha, presses enter), this string would be broadcasted to all the other clients and the server would respond by broadcasting the string "Alpha has been recieved." to all of the clients as well. My exact problem (to my knowledge) is that I can't do any kind of string comparison with the messages my event listener receives. Is it possible to extract the text entered by my chatroom members from their messages?


Where is your 'sock.on('data', function(data) {})' handler? I think the HTTP example is actually what you are looking for, listed below.

Example (for TCP Server):

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

Example for HTTP Server:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜