Skip to content

Instantly share code, notes, and snippets.

@heroic
Created February 5, 2011 10:07
Show Gist options
  • Save heroic/812343 to your computer and use it in GitHub Desktop.
Save heroic/812343 to your computer and use it in GitHub Desktop.
node-mysql query issue
var sys = require('sys');
var io = require('socket.io');
var http = require('http');
var db_client = require("mysql").Client;
var db = new db_client();
db.user = "root";
db.password = "hello"
db.connect();
db.query("USE development_test");
server = http.createServer(function(req, res) {
// your normal server code
// res.writeHead(200, {'Content-Type': 'text/html'});
// res.end('<h1>Hello world</h1>');
});
server.listen(8080);
var socket = io.listen(server);
var clients = [];
Array.prototype.each = function(fn) {
for(i=0;i< this.length;i++)
fn(i,this[i])
}
socket.on('connection', function(client){
// new client is here!
client.on('message', function(msg) {
if(client.user_id == null) {
client.user_id = msg.user_id
// now we need to subscribe u to people u r both way frnds with
r = db.query("select user_id from friends where user_id IN (select to_id from friends where user_id = " + client.user_id + ") and to_id = " + client.user_id, function(err, results) {
if (err) {
throw err;
}
console.log(results);
});
clients.push(client);
}
else if(msg.text != null) {
//we are chatting now...
client.broadcast({
text: msg.text
});
}
});
client.on('disconnect', function() {
clients.each(function(index){
if(clients[index].sessionId == client.sessionId)
clients.splice(index,1);
});
});
});
db.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment