JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fec346737b5f7f1b9ad3c66758a20995d7ca33e8
[watch-my-terminal.git] / publish-my-session.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 io.sockets.on 'connection', (socket) ->
30         # FIXME socket.emit 'write', term.getState()
31         term.on 'sequence', (data) ->
32                 socket.emit 'write', "sequence: #{data}"
33         term.on 'text', (data) ->
34                 socket.emit 'write', data
35         socket.on 'disconnect', ->
36                 # FIXME stop term update callback
37                 console.log 'client disconnected'
38
39 process.stdin.resume()
40 process.stdin.setEncoding 'utf8'
41
42 process.stdin.on 'data', (data) ->
43         term.update data
44
45 process.stdin.on 'end', -> process.exit()