JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix and display colors, bold and underline
[watch-my-terminal.git] / server.coffee
1 handler = (req, res) ->
2         err = (req, res) ->
3                 res.writeHead(404, 'Content-Type': 'text/plain')
4                 return res.end("Error loading #{req.url}")
5         switch req.url
6                 when '/', '/index.html'
7                         filename = __dirname + '/index.html'
8                         type = 'text/html'
9                 when '/jquery.js'
10                         filename = '/usr/share/javascript/jquery/jquery.min.js'
11                         type = 'text/javascript'
12                 else
13                         return err req, res
14         fs.readFile filename, (err, data) ->
15                 return err req, res if err
16
17                 res.writeHead(200, 'Content-Type': type)
18                 res.end(data)
19
20 app = require('http').createServer(handler)
21 io = require('socket.io').listen(app)
22 fs = require('fs')
23 terminal = require('./terminal.coffee')
24
25 # SETTINGS
26 app.listen(9293)
27 term = terminal.new(105, 66)
28
29 sockets = []
30
31 io.sockets.on 'connection', (socket) ->
32         sockets.push socket
33         socket.on 'disconnect', ->
34                 for i in [i...sockets.length]
35                         if sockets[i] is socket
36                                 sockets.splice i, 1
37                                 return
38
39         socket.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes
40
41 process.stdin.resume()
42 process.stdin.setEncoding 'utf8'
43
44 process.stdin.on 'data', (data) ->
45         term.update data
46         # FIXME send data, and have client parse it
47         for s in sockets
48                 s.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes
49
50 process.stdin.on 'end', -> process.exit()