handler = (req, res) -> err = (req, res) -> res.writeHead(404, 'Content-Type': 'text/plain') return res.end("Error loading #{req.url}") switch req.url when '/', '/index.html' filename = __dirname + '/index.html' type = 'text/html' when '/jquery.js' filename = '/usr/share/javascript/jquery/jquery.min.js' type = 'text/javascript' else return err req, res fs.readFile filename, (err, data) -> return err req, res if err res.writeHead(200, 'Content-Type': type) res.end(data) app = require('http').createServer(handler) io = require('socket.io').listen(app) fs = require('fs') terminal = require('./terminal.coffee') # SETTINGS app.listen(9293) term = terminal.new(105, 66) sockets = [] io.sockets.on 'connection', (socket) -> sockets.push socket socket.on 'disconnect', -> for i in [i...sockets.length] if sockets[i] is socket sockets.splice i, 1 return socket.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes process.stdin.resume() process.stdin.setEncoding 'utf8' process.stdin.on 'data', (data) -> term.update data # FIXME send data, and have client parse it for s in sockets s.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes process.stdin.on 'end', -> process.exit()