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(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);
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):
Example for HTTP Server: