forked from Scarlet-Fox/lamia-community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.coffee
73 lines (59 loc) · 1.95 KB
/
listener.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
express = require 'express'
app = express()
fs = require 'fs'
bodyParser = require('body-parser')
app.use(bodyParser.json())
key = "32932mklfdsy972@212278"
socketList = new Array()
config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
server = require('http').createServer(app)
io = require('socket.io')(server, {path: config.listener_path})
app.use (req, res, next) ->
do next
app.get "/talker", (req, res) ->
res.send ''
app.post "/talker/notify", (req, res) ->
res.send 'ok'
for client in socketList
try
if client.user in req.body.users
req_temp = JSON.parse(JSON.stringify(req.body))
if req_temp.count_update?
req_temp = req_temp
else
req_temp.count = req_temp.count[client.user]
req_temp.dashboard_count = req_temp.dashboard_count[client.user]
req_temp.id = req_temp.id[client.user]
req_temp._id = req_temp.id
client.emit "notify", req_temp
catch
continue
io.on 'connection', (client) ->
client.on "user", (data) ->
socketList.push client
if socketList.length > 10000
socketList.shift()
client.user = data.user
client.on "join", (data) ->
client.join data
client.on "disconnect", () ->
for socket, i in socketList
try
if socket.id == client.id
socketList.splice(i, 1)
catch
continue
client.on "event", (data) ->
room = data.room
client.broadcast.to(room).emit("event", data)
app.listen 3001, () ->
console.log('Listener at http://%s:%s', server.address().address, 3001)
server.listen 3000, () ->
host = server.address().address
port = server.address().port
console.log('Listener at http://%s:%s', host, 3000)
# var clients_in_the_room = io.sockets.adapter.rooms[roomId];
# for (var clientId in clients_in_the_room ) {
# console.log('client: %s', clientId); //Seeing is believing
# var client_socket = io.sockets.connected[clientId];//Do whatever you want with this
# }