-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (145 loc) · 5.27 KB
/
index.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import jamulusRpcInterface from './jamulusrpcclient/RPCmodule.mjs';
import express from 'express';
import * as http from 'http';
import { Server } from "socket.io";
const app = express();
const server = http.createServer(app);
const io = new Server(server);
let port = process.env.JAMCHATPORT || 32123;
let secret = process.env.JSONRPCSECRETFILE || 'jamulusRPCSecret.txt';
let rpcPort = process.env.JSONRPCPORT || 8765;
let streamUrl = process.env.STREAMURL;
process.argv.slice(2).forEach((val) => {
val = val.split('=')
switch (val[0]) {
case 'jamChatHttpPort':
port = val[1];
break;
case 'jamulusRPCSecretFilePath':
secret = val[1];
break;
case 'jamRPCPort':
rpcPort = val[1];
break;
case 'jamStreamLink':
streamUrl = val[1];
break;
default:
break;
}
});
const RPC = new jamulusRpcInterface(rpcPort, secret);
var connectedClients = {};
let partJson = '';
app.use(express.static('./public'))
RPC.jamRPCServer.on('data', (data) => {
data = data.toString().split('\n');
for (const row of data) {
let parsed = {};
if (row && !row.error) {
try {
parsed = JSON.parse(row);
} catch (e) {
if (e instanceof SyntaxError) {
if (e.message.split(' ')[1] == 'end') {
console.log(`${e.name}: ${e.message}`);
partJson = row;
continue;
}
else if (e.message.split(' ')[1] == 'token') {
console.log(`${e.name}: ${e.message}`);
partJson += row;
try {
parsed = JSON.parse(partJson);
partJson = '';
console.log('successfully parsed')
} catch (e) {
continue;
}
}
}
}
if (parsed.id && parsed.id == 'getInfo' && parsed.result) {
processData(parsed.result.clients);
continue;
}
switch (parsed.method) {
case 'jamulusserver/chatMessageReceived':
io.emit('chat', parsed.params.chatMessage)
break;
case 'jamulusserver/clientConnected':
setTimeout(function() {
RPC.jamRPCServer.write('{"id":"getInfo","jsonrpc":"2.0","method":"jamulusserver/getClientDetails","params":{}}\n');
}, 500);
break;
case 'jamulusserver/clientDisconnected':
setTimeout(function() {
RPC.jamRPCServer.write('{"id":"getInfo","jsonrpc":"2.0","method":"jamulusserver/getClientDetails","params":{}}\n');
}, 500);
break;
default:
break;
}
}
}
});
io.on('connection', socket => {
socket.on('chat', (user, message) => {
if (socket.id in connectedClients === true) {
createAndSendBuffer(user, message.replace(/"/g,'\''))
} else {
RPC.jamRPCServer.write('{"id":"getInfo","jsonrpc":"2.0","method":"jamulusserver/getClientDetails","params":{}}\n');
socket.emit('resetUsersView')
registerUser(socket, user)
}
})
socket.on("disconnecting", () => {
if (connectedClients[socket.id] != undefined ) {
io.emit('userDisconnected', connectedClients[socket.id][0])
delete connectedClients[socket.id]
}
})
})
class User {
constructor(name, ip, city, country, instrument, instrumentPicture, skill) {
this.name = name;
this.ip = ip;
this.city = city;
this.country = country;
this.instrument = instrument;
this.instrumentPicture = instrumentPicture;
this.skill = skill;
}
}
const processData = (data) => {
let result = '<tr><th>name</th><th>instrument</th><th>city</th><th>country</th><th>skill</th></tr>';
data.forEach( element => {
result += '<tr>';
result += '<td>'+ element.name +'</td>';
result += '<td>'+ element.instr +'</td>';
result += '<td>'+ element.city +'</td>';
result += '<td>'+ element.country +'</td>';
result += '<td>'+ element.skill +'</td>';
result += '</tr>';
});
io.emit('users', result);
}
const registerUser = (socket, userName) => {
socket.emit('resetUsersView')
io.emit('userConnected', userName)
for (const [id, [name, ip]] of Object.entries(connectedClients)) {
socket.emit('userConnected', name);
}
connectedClients[socket.id] = [ userName, socket.client.conn.remoteAddress ]
// console.log(connectedClients);
}
const createAndSendBuffer = (user, message) => {
message = '<b>***Message from listener ' + user + ':</b> ' + message;
RPC.jamRPCServer.write('{"id":"chat","jsonrpc":"2.0","method":"jamulusserver/broadcastChatMessage","params":{"chatMessage":"' + message + '"}}\n');
}
app.get('/config', (req, res) => {
res.json({ streamUrl });
});
server.listen(port, () => {
console.log(`Server running on port: ${port}`)
});