Fixed light client & added Docker config
This commit is contained in:
2
server/.dockerignore
Normal file
2
server/.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
13
server/Dockerfile
Normal file
13
server/Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
||||
FROM node:10
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 8080
|
||||
EXPOSE 9090
|
||||
CMD [ "npm", "start" ]
|
||||
38
server/client/index.html
Normal file
38
server/client/index.html
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
<title>Lil'Streamy</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="loginDiv">
|
||||
<input class="input" id="loginInput" placeholder="username" type="text"/>
|
||||
<button class="button" id="loginBt" >Login</button>
|
||||
</div>
|
||||
|
||||
<div id="connectDiv">
|
||||
<input id="callInput" placeholder="user to call" type="text"/>
|
||||
<button id="callBt" >Connect</button>
|
||||
<hr>
|
||||
<input id="videoInput" type="file" accept="video/*"/>
|
||||
</div>
|
||||
|
||||
<div id="videoDiv">
|
||||
<h2 id="videoTitle"></h2>
|
||||
<video autoplay controls id="video"></video>
|
||||
<hr>
|
||||
<button id="disconnectBt">Disconnect</button>
|
||||
</div>
|
||||
|
||||
<div id="loadDiv">
|
||||
<div class="loader" style="display:block"></div>
|
||||
</div>
|
||||
|
||||
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
|
||||
<script src = "scripts/script.js"></script>
|
||||
<script src = "scripts/rtc.js"></script>
|
||||
<script src = "scripts/signal.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
200
server/client/scripts/rtc.js
Normal file
200
server/client/scripts/rtc.js
Normal file
@@ -0,0 +1,200 @@
|
||||
var name
|
||||
var connections = new Map()
|
||||
|
||||
const configuration = {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
|
||||
const offerOptions = {
|
||||
offerToReceiveAudio: 1,
|
||||
offerToReceiveVideo: 1
|
||||
}
|
||||
|
||||
function handleLogin(success) {
|
||||
if (success === false) {
|
||||
alert('try a different username')
|
||||
} else {
|
||||
loginDiv.style.display = 'none'
|
||||
connectDiv.style.display = 'block'
|
||||
}
|
||||
}
|
||||
|
||||
async function createPeerConnection(target) {
|
||||
console.log('CREATED PEER CONNECTION')
|
||||
var connection = new RTCPeerConnection(configuration)
|
||||
connections[target] = connection
|
||||
|
||||
connection.onicecandidate = function(event) {
|
||||
if (event.candidate) {
|
||||
send({
|
||||
type: 'candidate',
|
||||
name: name,
|
||||
target: target,
|
||||
candidate: event.candidate
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
connection.onnegotiationneeded = function() { handleNegotiationNeededEvent(target) }
|
||||
connection.ontrack = function(event) { handleTrackEvent(event) }
|
||||
connection.onsignalingstatechange = function() { handleSignalingStateChangeEvent(connection) }
|
||||
connection.oniceconnectionstatechange = function() { handleICEConnectionStateChangeEvent(connection) }
|
||||
connection.onicegatheringstatechange = function() { handleICEGatheringStateChangeEvent(connection) }
|
||||
|
||||
// window.setInterval(getConnectionStats, 1000)
|
||||
}
|
||||
|
||||
function handleICEConnectionStateChangeEvent(connection) {
|
||||
console.log('ICE CONNECTION CHANGE ' + connection.iceConnectionState)
|
||||
}
|
||||
|
||||
function handleICEGatheringStateChangeEvent(connection) {
|
||||
console.log('ICE GATHERING CHANGE ' + connection.iceGatheringState)
|
||||
}
|
||||
|
||||
async function makeOffer(target) {
|
||||
createPeerConnection(target)
|
||||
|
||||
var connection = connections[target]
|
||||
|
||||
var offer = await connection.createOffer()
|
||||
send({
|
||||
type: 'offer',
|
||||
name: name,
|
||||
target: target,
|
||||
offer: offer
|
||||
})
|
||||
await connection.setLocalDescription(offer)
|
||||
|
||||
}
|
||||
|
||||
async function handleOffer(offer, target) {
|
||||
console.log('GOT OFFER FROM ' + target)
|
||||
await createPeerConnection(target)
|
||||
|
||||
var connection = connections[target]
|
||||
|
||||
await connection.setRemoteDescription(new RTCSessionDescription(offer))
|
||||
|
||||
if (stream) {
|
||||
console.log('STREAM DETECTED')
|
||||
stream.getTracks().forEach((track) => {
|
||||
console.log('ADDED TRACK')
|
||||
connection.addTrack(track, stream)
|
||||
})
|
||||
}
|
||||
|
||||
var answer = await connection.createAnswer()
|
||||
|
||||
await connection.setLocalDescription(answer)
|
||||
|
||||
send({
|
||||
type: 'answer',
|
||||
name: name,
|
||||
target: target,
|
||||
answer: answer
|
||||
})
|
||||
|
||||
videoTitle.innerHTML = name + ' | ' + connections.size + ' users connected'
|
||||
}
|
||||
|
||||
async function handleAnswer(answer, target) {
|
||||
console.log('GOT ANSWER FROM ' + target)
|
||||
var connection = connections[target]
|
||||
await connection.setRemoteDescription(new RTCSessionDescription(answer))
|
||||
}
|
||||
|
||||
async function handleCandidate(candidate, target) {
|
||||
console.log('GOT CANDIDATE FROM ' + target)
|
||||
var connection = connections[target]
|
||||
await connection.addIceCandidate(new RTCIceCandidate(candidate))
|
||||
}
|
||||
|
||||
async function handleNegotiationNeededEvent(target) {
|
||||
console.log('NEGOTIATION NEEDED FROM ' + target)
|
||||
|
||||
var connection = connections[target]
|
||||
var offer = await connection.createOffer(offerOptions)
|
||||
|
||||
await connection.setLocalDescription(offer)
|
||||
|
||||
send({
|
||||
type: 'video-offer',
|
||||
name: name,
|
||||
target: target,
|
||||
sdp: connection.localDescription
|
||||
})
|
||||
}
|
||||
|
||||
function handleLeave() {
|
||||
connections.forEach((connection) => {
|
||||
connection.close()
|
||||
connection = null
|
||||
})
|
||||
connections = new Map()
|
||||
|
||||
remoteVideo.pause()
|
||||
remoteVideo.src = null
|
||||
}
|
||||
|
||||
function handleUserlist(list) {
|
||||
console.log('GOT USER LIST')
|
||||
|
||||
}
|
||||
|
||||
async function handleVideoOffer(sdp, target) {
|
||||
console.log('GOT VIDEO OFFER FROM ' + target)
|
||||
await createPeerConnection(target)
|
||||
var connection = connections[target]
|
||||
await connection.setRemoteDescription(new RTCSessionDescription(sdp))
|
||||
|
||||
if (stream) {
|
||||
console.log('STREAM DETECTED')
|
||||
stream.getTracks().forEach((track) => {
|
||||
console.log('ADDED TRACK')
|
||||
connection.addTrack(track, stream)
|
||||
})
|
||||
}
|
||||
|
||||
var answer = await connection.createAnswer()
|
||||
await connection.setLocalDescription(answer)
|
||||
|
||||
send({
|
||||
type: 'video-answer',
|
||||
name: name,
|
||||
target: target,
|
||||
sdp: answer
|
||||
})
|
||||
|
||||
//var keys = connections.keys().next().value
|
||||
videoTitle.innerHTML = name + ' | connected to '
|
||||
}
|
||||
|
||||
async function handleVideoAnswer(sdp, target) {
|
||||
console.log('GOT VIDEO ANSWER FROM ' + target)
|
||||
|
||||
var connection = connections[target]
|
||||
await connection.setRemoteDescription(new RTCSessionDescription(sdp))
|
||||
}
|
||||
|
||||
async function handleSignalingStateChangeEvent(connection) {
|
||||
console.log('STATE CHANGED TO : ' + connection.signalingState)
|
||||
switch(connection.signalingState) {
|
||||
case 'closed':
|
||||
await connection.close()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function handleTrackEvent(event) {
|
||||
console.log('GOT TRACK')
|
||||
remoteVideo.srcObject = event.streams[0]
|
||||
videoDiv.style.display = 'block'
|
||||
loadDiv.style.display = 'none'
|
||||
}
|
||||
|
||||
function getConnectionStats() {
|
||||
connections.forEach((connection, target) => {
|
||||
console.log('[' + target + '] ' + connection.connectionState)
|
||||
})
|
||||
}
|
||||
64
server/client/scripts/script.js
Normal file
64
server/client/scripts/script.js
Normal file
@@ -0,0 +1,64 @@
|
||||
const loginDiv = document.querySelector('#loginDiv')
|
||||
const loginInput = document.querySelector('#loginInput')
|
||||
const loginBt = document.querySelector('#loginBt')
|
||||
|
||||
const connectDiv = document.querySelector('#connectDiv')
|
||||
const callInput = document.querySelector('#callInput')
|
||||
const callBt = document.querySelector('#callBt')
|
||||
const videoInput = document.querySelector('#videoInput')
|
||||
|
||||
const videoDiv = document.querySelector('#videoDiv')
|
||||
const videoTitle = document.querySelector('#videoTitle')
|
||||
const remoteVideo = document.querySelector('#video')
|
||||
const disconnectBt = document.querySelector('#disconnectBt')
|
||||
|
||||
const loadDiv = document.querySelector('#loadDiv')
|
||||
|
||||
var stream
|
||||
|
||||
loginDiv.style.display = 'block'
|
||||
|
||||
loginBt.addEventListener('click', function (event) {
|
||||
name = loginInput.value
|
||||
|
||||
if (name.length > 0) {
|
||||
send({
|
||||
type: 'login',
|
||||
name: name
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
callBt.addEventListener('click', function () {
|
||||
var callToUsername = callInput.value;
|
||||
|
||||
if (callToUsername.length > 0) {
|
||||
makeOffer(callToUsername)
|
||||
loadDiv.style.display = 'block'
|
||||
connectDiv.style.display = 'none'
|
||||
}
|
||||
})
|
||||
|
||||
disconnectBt.addEventListener('click', function () {
|
||||
send({
|
||||
type: 'leave',
|
||||
name: name
|
||||
})
|
||||
handleLeave()
|
||||
videoDiv.style.display = 'none'
|
||||
loginDiv.style.display = 'block'
|
||||
})
|
||||
|
||||
videoInput.addEventListener('change', function (event) {
|
||||
remoteVideo.src = URL.createObjectURL(this.files[0])
|
||||
|
||||
videoDiv.style.display = 'block'
|
||||
connectDiv.style.display = 'none'
|
||||
videoTitle.innerHTML = name + ' | ' + connections.size + ' user connected'
|
||||
})
|
||||
|
||||
remoteVideo.onplay = function () {
|
||||
console.log('ADD STREAM')
|
||||
if(remoteVideo.mozCaptureStream()) stream = remoteVideo.mozCaptureStream()
|
||||
else stream = remoteVideo.captureStream()
|
||||
}
|
||||
58
server/client/scripts/signal.js
Normal file
58
server/client/scripts/signal.js
Normal file
@@ -0,0 +1,58 @@
|
||||
//connecting to our signaling server
|
||||
var conn = new WebSocket('ws://gltronic.ovh/lilstreamy/ws');
|
||||
|
||||
conn.onopen = function () {
|
||||
console.log('Connected to the signaling server')
|
||||
}
|
||||
|
||||
conn.onmessage = function (msg) {
|
||||
console.log('Got message', msg.data);
|
||||
|
||||
var data = JSON.parse(msg.data);
|
||||
|
||||
switch (data.type) {
|
||||
case 'login':
|
||||
handleLogin(data.success)
|
||||
break
|
||||
|
||||
case 'offer':
|
||||
handleOffer(data.offer, data.name)
|
||||
break
|
||||
|
||||
case 'answer':
|
||||
handleAnswer(data.answer, data.name)
|
||||
break
|
||||
|
||||
case 'candidate':
|
||||
handleCandidate(data.candidate, data.name)
|
||||
break
|
||||
|
||||
case 'userlist':
|
||||
handleUserlist(data)
|
||||
break
|
||||
|
||||
case 'leave':
|
||||
handleLeave()
|
||||
break
|
||||
|
||||
case 'video-offer':
|
||||
handleVideoOffer(data.sdp, data.name)
|
||||
break
|
||||
|
||||
case 'video-answer':
|
||||
handleVideoAnswer(data.sdp, data.name)
|
||||
break
|
||||
|
||||
default:
|
||||
break
|
||||
}
|
||||
};
|
||||
|
||||
conn.onerror = function (err) {
|
||||
console.log('Got error', err)
|
||||
}
|
||||
|
||||
function send(message) {
|
||||
console.log('Sended message', message)
|
||||
conn.send(JSON.stringify(message))
|
||||
}
|
||||
49
server/client/style.css
Normal file
49
server/client/style.css
Normal file
@@ -0,0 +1,49 @@
|
||||
body {
|
||||
color: #ffffff;
|
||||
background-color: #23232e;
|
||||
font: 14px normal Arial, Helvetica, sans-serif;
|
||||
z-index: -4;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
div {
|
||||
display: none;
|
||||
margin: auto;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
video {
|
||||
width: 50%;
|
||||
}
|
||||
|
||||
#loadDiv {
|
||||
display: none;
|
||||
position: fixed;
|
||||
z-index: 1;
|
||||
padding-top: 100px;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: auto;
|
||||
background-color: rgb(0,0,0);
|
||||
background-color: rgba(0,0,0,0.4);
|
||||
}
|
||||
|
||||
.loader {
|
||||
margin: auto;
|
||||
padding: 20px;
|
||||
border: 16px solid #f3f3f3;
|
||||
border-top: 16px solid #3498db;
|
||||
border-bottom: 16px solid violet;
|
||||
border-radius: 50%;
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
animation: spin 0.5s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
{
|
||||
"port" : 8080
|
||||
"port" : 8080,
|
||||
"ws_port" : 9090
|
||||
}
|
||||
@@ -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