Created
February 5, 2011 10:07
-
-
Save heroic/812343 to your computer and use it in GitHub Desktop.
node-mysql query issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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