Fixed light client & added Docker config
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require("body-parser");
|
||||
const signal = require("./signal3");
|
||||
const cors = require("cors");
|
||||
const config = require("../config.json");
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const cors = require('cors')
|
||||
const config = require('../config.json')
|
||||
|
||||
require('./signal')
|
||||
|
||||
const app = express()
|
||||
const {port} = config
|
||||
@@ -11,7 +12,7 @@ app.use(cors())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.use(express.static("../clientV"))
|
||||
app.use(express.static('./client'))
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Launching Lil'Streamy on ${port}`)
|
||||
|
||||
@@ -1,154 +1,102 @@
|
||||
const WebSocketServer = require('ws').Server;
|
||||
const WebSocketServer = require('ws').Server
|
||||
|
||||
var wss = new WebSocketServer({port: 9090});
|
||||
var wss = new WebSocketServer({port: 9090})
|
||||
|
||||
var users = {};
|
||||
var users = {}
|
||||
|
||||
wss.on('connection', function(connection) {
|
||||
console.log('User connected')
|
||||
|
||||
console.log("User connected");
|
||||
|
||||
//when server gets a message from a connected user
|
||||
connection.on('message', (message) => onMessage(connection, message));
|
||||
|
||||
//when user exits, for example closes a browser window
|
||||
//this may help if we are still in "offer","answer" or "candidate" state
|
||||
connection.on("close", () => onClose(connection));
|
||||
});
|
||||
connection.on('message', (message) => onMessage(connection, message))
|
||||
|
||||
connection.on('close', () => onClose(connection))
|
||||
})
|
||||
|
||||
function sendTo(connection, message) {
|
||||
connection.send(JSON.stringify(message));
|
||||
connection.send(JSON.stringify(message))
|
||||
}
|
||||
|
||||
function onClose(connection) {
|
||||
if(connection.name) {
|
||||
delete users[connection.name];
|
||||
|
||||
if(connection.otherName) {
|
||||
console.log("Disconnecting from ", connection.otherName);
|
||||
var conn = users[connection.otherName];
|
||||
conn.otherName = null;
|
||||
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: "leave"
|
||||
});
|
||||
}
|
||||
}
|
||||
if(connection.name) {
|
||||
delete users[connection.name]
|
||||
|
||||
if(connection.otherName) {
|
||||
console.log('Disconnecting from ', connection.otherName)
|
||||
var conn = users[connection.otherName]
|
||||
conn.otherName = null
|
||||
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: 'leave'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onMessage(connection, message) {
|
||||
var data;
|
||||
var data
|
||||
|
||||
//accepting only JSON messages
|
||||
try {
|
||||
data = JSON.parse(message);
|
||||
} catch (e) {
|
||||
console.log("Invalid JSON");
|
||||
data = {};
|
||||
}
|
||||
try {
|
||||
data = JSON.parse(message);
|
||||
} catch (e) {
|
||||
console.log('Invalid JSON')
|
||||
data = {}
|
||||
}
|
||||
|
||||
//switching type of the user message
|
||||
switch (data.type) {
|
||||
//when a user tries to login
|
||||
case "login":
|
||||
console.log("User logged", data.name);
|
||||
|
||||
//if anyone is logged in with this username then refuse
|
||||
if(users[data.name]) {
|
||||
sendTo(connection, {
|
||||
type: "login",
|
||||
success: false
|
||||
});
|
||||
} else {
|
||||
//save user connection on the server
|
||||
users[data.target] = connection;
|
||||
connection.name = data.target;
|
||||
|
||||
sendTo(connection, {
|
||||
type: "login",
|
||||
success: true
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "offer":
|
||||
//for ex. UserA wants to call UserB
|
||||
console.log("Sending offer to: ", data.target);
|
||||
|
||||
//if UserB exists then send him offer details
|
||||
var conn = users[data.target];
|
||||
|
||||
if(conn != null) {
|
||||
//setting that UserA connected with UserB
|
||||
connection.otherName = data.target;
|
||||
|
||||
sendTo(conn, {
|
||||
type: "offer",
|
||||
offer: data.offer,
|
||||
name: connection.target
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "answer":
|
||||
console.log("Sending answer to: ", data.target);
|
||||
//for ex. UserB answers UserA
|
||||
var conn = users[data.target];
|
||||
|
||||
if(conn != null) {
|
||||
connection.otherName = data.target;
|
||||
sendTo(conn, {
|
||||
type: "answer",
|
||||
answer: data.answer
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "candidate":
|
||||
console.log("Sending candidate to:",data.target);
|
||||
var conn = users[data.name];
|
||||
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: "candidate",
|
||||
candidate: data.candidate
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "leave":
|
||||
console.log("Disconnecting from", data.target);
|
||||
var conn = users[data.target];
|
||||
conn.otherName = null;
|
||||
|
||||
//notify the other user so he can disconnect his peer connection
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: "leave"
|
||||
});
|
||||
}
|
||||
break;
|
||||
switch (data.type) {
|
||||
case 'login' :
|
||||
console.log('User logged', data.name);
|
||||
if(users[data.name]) {
|
||||
sendTo(connection, {
|
||||
type: 'login',
|
||||
success: false
|
||||
})
|
||||
} else {
|
||||
users[data.name] = connection
|
||||
connection.name = data.name
|
||||
|
||||
sendTo(connection, {
|
||||
type: 'login',
|
||||
success: true
|
||||
})
|
||||
}
|
||||
break
|
||||
|
||||
case 'leave' :
|
||||
console.log('Disconnecting from', data.name)
|
||||
var conn = users[data.name]
|
||||
conn.otherName = null
|
||||
|
||||
case "video-offer":
|
||||
console.log("Send video offer to", data.target);
|
||||
var conn = users[data.name];
|
||||
|
||||
if(conn != null) {
|
||||
connection.otherName = data.target;
|
||||
sendTo(conn, {
|
||||
type: "video-offer",
|
||||
answer: data.answer
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
sendTo(connection, {
|
||||
type: "error",
|
||||
message: "Command not found: " + data.type
|
||||
});
|
||||
break;
|
||||
}
|
||||
//notify the other user so he can disconnect his peer connection
|
||||
/*
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: 'leave'
|
||||
});
|
||||
}*/
|
||||
break;
|
||||
|
||||
case 'userlist' :
|
||||
console.log('Send list to', data.name)
|
||||
sendTo(connection, {
|
||||
type: 'userlist',
|
||||
list: users
|
||||
});
|
||||
break
|
||||
|
||||
default:
|
||||
if (data.target){
|
||||
var targetConnection = users[data.target];
|
||||
if(targetConnection){
|
||||
console.log('Forward message from ' + data.name + ' to ' + data.target + ' (' + data.type + ')')
|
||||
sendTo(targetConnection, data)
|
||||
}
|
||||
} else {
|
||||
sendTo(connection, {
|
||||
type: 'error',
|
||||
message: 'Command not found: ' + data.type
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
const WebSocketServer = require('ws').Server;
|
||||
|
||||
var wss = new WebSocketServer({port: 9090});
|
||||
|
||||
var users = {};
|
||||
|
||||
wss.on('connection', function(connection) {
|
||||
console.log("User connected");
|
||||
|
||||
connection.on('message', (message) => onMessage(connection, message));
|
||||
|
||||
connection.on("close", () => onClose(connection));
|
||||
});
|
||||
|
||||
function sendTo(connection, message) {
|
||||
connection.send(JSON.stringify(message));
|
||||
}
|
||||
|
||||
function onClose(connection) {
|
||||
if(connection.name) {
|
||||
delete users[connection.name];
|
||||
|
||||
if(connection.otherName) {
|
||||
console.log("Disconnecting from ", connection.otherName);
|
||||
var conn = users[connection.otherName];
|
||||
conn.otherName = null;
|
||||
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: "leave"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onMessage(connection, message) {
|
||||
var data;
|
||||
|
||||
try {
|
||||
data = JSON.parse(message);
|
||||
} catch (e) {
|
||||
console.log("Invalid JSON");
|
||||
data = {};
|
||||
}
|
||||
|
||||
switch (data.type) {
|
||||
case "login" :
|
||||
console.log("User logged", data.name);
|
||||
if(users[data.name]) {
|
||||
sendTo(connection, {
|
||||
type: "login",
|
||||
success: false
|
||||
});
|
||||
} else {
|
||||
users[data.name] = connection;
|
||||
connection.name = data.name;
|
||||
|
||||
sendTo(connection, {
|
||||
type: "login",
|
||||
success: true
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
case "leave" :
|
||||
console.log("Disconnecting from", data.name);
|
||||
var conn = users[data.name];
|
||||
conn.otherName = null;
|
||||
|
||||
//notify the other user so he can disconnect his peer connection
|
||||
/*
|
||||
if(conn != null) {
|
||||
sendTo(conn, {
|
||||
type: "leave"
|
||||
});
|
||||
}*/
|
||||
break;
|
||||
|
||||
case "userlist" :
|
||||
console.log("Send list to", data.name);
|
||||
sendTo(connection, {
|
||||
type: "userlist",
|
||||
list: users
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
if (data.target){
|
||||
var targetConnection = users[data.target];
|
||||
console.log("Forward message from " + data.name + " to " + data.target + " (" + data.type + ")");
|
||||
sendTo(targetConnection, data);
|
||||
} else {
|
||||
sendTo(connection, {
|
||||
type: "error",
|
||||
message: "Command not found: " + data.type
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user