mirror of
https://github.com/mii443/libdatachannel.git
synced 2025-09-03 15:49:28 +00:00
Added dummy HTTP answerer
This commit is contained in:
@ -25,32 +25,41 @@ const clients = {};
|
||||
|
||||
const httpServer = http.createServer((req, res) => {
|
||||
console.log(`${req.method.toUpperCase()} ${req.url}`);
|
||||
|
||||
const respond = (code, data, contentType = 'text/plain') => {
|
||||
res.writeHead(code, {
|
||||
'Content-Type' : contentType,
|
||||
'Access-Control-Allow-Origin' : '*',
|
||||
});
|
||||
res.end(data);
|
||||
};
|
||||
|
||||
respond(404, 'Not Found');
|
||||
});
|
||||
|
||||
const wsServer = new websocket.server({ httpServer });
|
||||
const wsServer = new websocket.server({httpServer});
|
||||
wsServer.on('request', (req) => {
|
||||
console.log(`WS ${req.resource}`);
|
||||
|
||||
const { path } = req.resourceURL;
|
||||
const {path} = req.resourceURL;
|
||||
const splitted = path.split('/');
|
||||
splitted.shift();
|
||||
const id = splitted[0];
|
||||
|
||||
const conn = req.accept(null, req.origin);
|
||||
conn.on('message', (data) => {
|
||||
if(data.type === 'utf8') {
|
||||
if (data.type === 'utf8') {
|
||||
console.log(`Client ${id} << ${data.utf8Data}`);
|
||||
|
||||
const message = JSON.parse(data.utf8Data);
|
||||
const destId = message.id;
|
||||
const dest = clients[destId];
|
||||
if(dest) {
|
||||
if (dest) {
|
||||
message.id = id;
|
||||
const data = JSON.stringify(message);
|
||||
console.log(`Client ${destId} >> ${data}`);
|
||||
dest.send(data);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
console.error(`Client ${destId} not found`);
|
||||
}
|
||||
}
|
||||
@ -68,7 +77,5 @@ const splitted = endpoint.split(':');
|
||||
const port = splitted.pop();
|
||||
const hostname = splitted.join(':') || '127.0.0.1';
|
||||
|
||||
httpServer.listen(port, hostname, () => {
|
||||
console.log(`Server listening on ${hostname}:${port}`);
|
||||
});
|
||||
|
||||
httpServer.listen(port, hostname,
|
||||
() => { console.log(`Server listening on ${hostname}:${port}`); });
|
||||
|
Reference in New Issue
Block a user