JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
plain text flows and wraps properly
[watch-my-terminal.git] / server.coffee
diff --git a/server.coffee b/server.coffee
new file mode 100644 (file)
index 0000000..3ff1529
--- /dev/null
@@ -0,0 +1,47 @@
+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
+
+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
+
+process.stdin.on 'end', -> process.exit()