Skip to content

Instantly share code, notes, and snippets.

@jankuca
Created September 14, 2012 21:13
Show Gist options
  • Save jankuca/3724881 to your computer and use it in GitHub Desktop.
Save jankuca/3724881 to your computer and use it in GitHub Desktop.
- (void)connect {
NSHost *host = [NSHost hostWithName:@"example.com"];
// iStream and oStream are instance variables
[NSStream getStreamsToHost:host port:2000 inputStream:&iStream
outputStream:&oStream];
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch (eventCode) {
case NSStreamEventHasSpaceAvailable:
if (stream == oStream) {
NSString *data = @"HALO"
const uint8_t *rawstring = (const uint8_t *)[data UTF8String];
[oStream write:rawstring maxLength:strlen(rawstring)];
}
}
}
var net = require('net');
var readMessage = function (socket, chunk) {
var data = chunk.toString('utf8');
var msg = {};
msg.id = /^([a-zA-Z0-9]+)/.exec(data);
msg.action = /^([A-Z]+)/.exec(data.substr(msg.id.length + 1));
msg.data = data.substr(msg.id.length + msg.action.length + 2);
msg.respond = function (status, data) {
socket.write(msg.id + ' ' + status + ' ' + (data || ''), 'utf8');
};
return msg;
}
var createServer = function (handler) {
var sockets = {};
var server = net.createServer(function (socket) {
var nonces = [];
socket.on('data', function (chunk) {
var msg = readMessage(socket, chunk);
switch (msg.action) {
case 'HALO':
var nonce = Math.random() * 255 + Date.now();
nonces.push(nonce);
msg.respond(200, nonce);
break;
case 'AUTH':
var parts = data.split(':'); // email, pwd, nonce, since
var nonce = parts[2];
var nonce_index = nonces.indexOf(nonce);
if (nonce_index === -1) {
return msg.respond(403, 'Invalid nonce');
}
nonces.splice(nonce_index, 1);
db.getUser(parts[0], function (user) {
var hash = crypto.createHash('sha1').update(user.pwd_hash + ':' + parts[2]).digest('hex');
if (hash === parts[1]) {
return socket.write(403, 'Wrong password');
}
socket.user_id = user.id;
var clients = sockets[user.id] || [];
clients.push(socket);
sockets[user.id] = clients;
socket.write(200);
handler(socket, Number(parts[3]));
});
break;
default:
msg.respond(405);
}
});
socket.on('close', function () {
if (socket.user_id) {
var clients = sockets[socket.user_id];
var index = clients.indexOf(socket);
clients.splice(index, 1);
if (clients.length === 0) {
delete sockets[user_id];
}
}
});
socket.send = function (msg) {
var data = JSON.stringify(msg);
socket.write('MSG ' + data);
};
});
server.send = function (user_id, msg) {
var clients = sockets[user_id] || [];
if (clients.length) {
var data = JSON.stringify(msg);
clients.forEach(function (socket) {
socket.write('MSG ' + data);
});
}
};
return server;
};
var server = createServer(function (socket, since) {
// get data since the specified timestamp
db.getMsgs({ user_id: socket.user_id, date: { $gt: since }}, function (msgs) {
msgs.forEach(function (msg) {
socket.send(msg);
});
});
});
server.listen(2000, function () {
// server is listening for new connections
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment