organize, fix server to handle multiple sockets correctly

This commit is contained in:
Andrew Kelley 2012-01-21 23:42:02 -08:00
parent 4a4881b7ac
commit ef15d25a70
6 changed files with 53 additions and 23 deletions

5
.gitignore vendored
View file

@ -1,4 +1 @@
partybeat2.pro.user
build/
*.pyc
mpc/conf.py
public/app.js

8
Makefile Normal file
View file

@ -0,0 +1,8 @@
.PHONY: build clean
build:
coffee -j public/app.js -c src/
clean:
rm -f public/app.js

19
README
View file

@ -15,7 +15,22 @@
coffee-script
mpd
== To run the server ==
== Installation ==
coffee server.coffee
1. Install and configure mpd
(TODO write docs for this)
2. Compile and install:
$ make
$ sudo make install (TODO)
3. (TODO) You can now run `groovebasind` to start the server.
== Developing ==
Compile upon save:
$ coffee -w -j public/app.js -c src/

33
server.coffee → groovebasind Normal file → Executable file
View file

@ -1,3 +1,5 @@
#!/usr/bin/env coffee
http = require 'http'
net = require 'net'
socketio = require 'socket.io'
@ -11,19 +13,6 @@ config =
host: 'localhost'
port: 6600
client = null
onMpdData = null
connectMpdClient = ->
client = net.connect config.mpd.port, config.mpd.host, ->
console.log 'client connected'
client.on 'data', onMpdData if onMpdData?
client.on 'end', ->
console.log 'client disconnected, reconnecting'
connectMpdClient()
connectMpdClient()
fileServer = new (static.Server) './public'
app = http.createServer((request, response) ->
request.addListener 'end', ->
@ -34,11 +23,21 @@ app = http.createServer((request, response) ->
io = socketio.listen(app)
io.set 'log level', config.log_level
io.sockets.on 'connection', (socket) ->
client = null
connectClient = ->
client = net.connect config.mpd.port, config.mpd.host, ->
console.log 'client connected'
client.on 'data', (data) ->
socket.emit 'FromMpd', data.toString()
client.on 'end', ->
console.log 'client disconnected, reconnecting'
connectClient()
connectClient()
socket.on 'ToMpd', (data) ->
console.log "[in] " + data
client.write data
onMpdData = (data) ->
socket.emit 'FromMpd', data.toString()
client.on 'data', onMpdData
socket.on 'disconnect', ->
client.removeAllListeners 'end'
client.end()

10
public/index.html Normal file
View file

@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Music</title>
<script type="text/javascript" src="vendor/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="vendor/socket.io/socket.io.min.js"></script>
</head>
<body>
</body>
</html>

1
src/app.coffee Normal file
View file

@ -0,0 +1 @@
window.foo = "blah"