-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
127 lines (106 loc) · 3.5 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
express = require 'express'
sockjs = require 'sockjs'
url = require 'url'
path = require 'path'
fs = require 'fs'
http = require 'http'
Queue = require './Queue'
QueueEntry = require './QueueEntry'
youtube = require './youtube'
Chat = require './Chat'
bodyParser = require 'body-parser'
args = process.argv.splice(2);
if args.length < 1
port = process.env.VCAP_APP_PORT || 8080
else
port = args[0]
port = Number(port)
if isNaN port
port = process.env.VCAP_APP_PORT || 8080
#Chat
chat = new Chat()
# Socket Server
currentConnections = 0;
sockServer = sockjs.createServer()
sockServer.on 'connection', (conn) ->
++currentConnections
queue.updateConnectionNumber currentConnections
#conn.on 'data', (message) ->
# conn.write message
conn.on 'close', () ->
console.log 'close'
--currentConnections
queue.updateConnectionNumber currentConnections
sendUpdate = () =>
conn.write JSON.stringify queue.getQueue()
sendCurrentVideo = () =>
conn.write JSON.stringify queue.getCurrentVideo()
queue.on 'update', sendUpdate
queue.on 'currentVideo', sendCurrentVideo
queue.on 'noVideo', () =>
json = {type: 'loadVideo', content:null}
conn.write JSON.stringify json
queue.on 'skipCount', (needed, users) =>
json = {type: 'skipCount', content:{needed:needed, users:users}}
conn.write JSON.stringify json
chat.on 'chat', (user, message) =>
json = {type: 'chat', content:{user:user, message:message}}
conn.write JSON.stringify json
# Send updates
if queue.hasCurrentVideo()
sendCurrentVideo()
queue.updateSkip()
sendUpdate()
# Start queue
queue = new Queue()
# Generic add Video function
addVideo = (title, user) ->
youtube title, (id, title, length, image) =>
console.log id
if videoInQueue = queue.getVideoById id
videoInQueue.upvote user
queue.sendPlaylistUpdate()
chat.sysMessage user, "has upvoted "+videoInQueue.title
else
entry = new QueueEntry id, title, length, image
entry.upvote user
queue.addVideo entry
chat.sysMessage user, "has added "+title
() =>
console.log 'YouTube error callback'
app = express();
server = http.createServer(app);
app.use(bodyParser.urlencoded())
app.use(express.static(__dirname + '/http'));
sockServer.installHandlers server, {prefix:'/sock'}
console.log 'Listening on port', port
server.listen(port);
app.post '/add', (req, res) ->
if (req.body.title && req.body.user)
addVideo(req.body.title, req.body.user);
res.send('OK');
return
res.status(400).send('Nope');
app.post '/upvote', (req, res) ->
if (req.body.id && req.body.user)
video = queue.getVideoById(req.body.id);
if (video)
video.upvote query.user
queue.sendPlaylistUpdate();
chat.sysMessage query.user, "has upvoted "+video.title
res.send('OK');
return
res.status(400).send('Nope');
app.post '/chat', (req, res) ->
if (req.body.user && req.body.msg)
chat.chat req.body.user, req.body.msg
res.send('OK');
return
res.status(400).send('Nope');
app.post '/skip', (req, res) ->
if (req.body.user)
queue.skipCurrent req.body.user
chat.sysMessage req.body.user, "has voted to skip the current video"
res.send('OK');
return
res.status(400).send('Nope');