X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=server.coffee;h=5d5ca3539148fa7f336b0d1401c4b6df0304b053;hb=3fbdc55777e4eaa22c12340eb2f065e991ac4522;hp=1d0422b71a6e47fb2e4063eff70506cfe89e5e6f;hpb=394a14939b00dbbb2a30c8a87ac3333a41c74ef2;p=watch-my-terminal.git diff --git a/server.coffee b/server.coffee index 1d0422b..5d5ca35 100644 --- a/server.coffee +++ b/server.coffee @@ -1,7 +1,7 @@ handler = (req, res) -> - err = (req, res) -> - res.writeHead(404, 'Content-Type': 'text/plain') - return res.end("Error loading #{req.url}") + reply_err = (req, res, msg) -> + res.writeHead(200, 'Content-Type': 'text/plain') + return res.end("Error loading #{req.url} \"#{msg}\"") switch req.url when '/', '/index.html' filename = __dirname + '/index.html' @@ -9,14 +9,27 @@ handler = (req, res) -> when '/jquery.js' filename = '/usr/share/javascript/jquery/jquery.min.js' type = 'text/javascript' + when '/terminal.js' + filename = __dirname + '/terminal.coffee' + type = 'text/javascript' + when '/client.js' + filename = __dirname + '/client.coffee' + type = 'text/javascript' else - return err req, res - fs.readFile filename, (err, data) -> - return err req, res if err + return reply_err req, res + fs.readFile filename, 'utf8', (err, data) -> + return reply_err req, res, err if err + + if filename.substr(filename.length - 7) is '.coffee' + try + data = coffee.compile data + catch e + return reply_err req, res, "server faild to compile #{filename}: #{JSON.stringify(e)}" res.writeHead(200, 'Content-Type': type) res.end(data) +coffee = require 'coffee-script' app = require('http').createServer(handler) io = require('socket.io').listen(app) fs = require('fs') @@ -24,7 +37,7 @@ terminal = require('./terminal.coffee') # SETTINGS app.listen(9293) -term = terminal.new(105, 66) +term = terminal.new(104, 66) sockets = [] @@ -36,15 +49,14 @@ io.sockets.on 'connection', (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 + socket.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes, cursor_visible: term.cursor_visible, scroll_top: term.scroll_top, scroll_bottom: term.scroll_bottom 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 + s.emit 'data', data process.stdin.on 'end', -> process.exit()