JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add cursor incrementals movement: csi_[ABCD]
[watch-my-terminal.git] / server.coffee
1 handler = (req, res) ->
2         reply_err = (req, res, msg) ->
3                 res.writeHead(200, 'Content-Type': 'text/plain')
4                 return res.end("Error loading #{req.url} \"#{msg}\"")
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                 when '/terminal.js'
13                         filename = __dirname + '/terminal.coffee'
14                         type = 'text/javascript'
15                 when '/client.js'
16                         filename = __dirname + '/client.coffee'
17                         type = 'text/javascript'
18                 else
19                         return reply_err req, res
20         fs.readFile filename, 'utf8', (err, data) ->
21                 return reply_err req, res, err if err
22
23                 if filename.substr(filename.length - 7) is '.coffee'
24                         try
25                                 data = coffee.compile data
26                         catch e
27                                 return reply_err req, res, "server faild to compile #{filename}: #{JSON.stringify(e)}"
28
29                 res.writeHead(200, 'Content-Type': type)
30                 res.end(data)
31
32 coffee = require 'coffee-script'
33 app = require('http').createServer(handler)
34 io = require('socket.io').listen(app)
35 fs = require('fs')
36 terminal = require('./terminal.coffee')
37
38 # SETTINGS
39 app.listen(9293)
40 term = terminal.new(104, 66)
41
42 sockets = []
43
44 io.sockets.on 'connection', (socket) ->
45         sockets.push socket
46         socket.on 'disconnect', ->
47                 for i in [i...sockets.length]
48                         if sockets[i] is socket
49                                 sockets.splice i, 1
50                                 return
51
52         socket.emit 'init', width: term.width, height: term.height, x: term.x, y: term.y, a: term.a, text: term.text, attributes: term.attributes
53
54 process.stdin.resume()
55 process.stdin.setEncoding 'utf8'
56
57 process.stdin.on 'data', (data) ->
58         term.update data
59         for s in sockets
60                 s.emit 'data', data
61
62 process.stdin.on 'end', -> process.exit()