JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add cursor incrementals movement: csi_[ABCD]
[watch-my-terminal.git] / server.coffee
index 3ff1529..d66b6cd 100644 (file)
@@ -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,12 +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
+
 process.stdin.resume()
 process.stdin.setEncoding 'utf8'
 
 process.stdin.on 'data', (data) ->
        term.update data
        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()