Converted to https with self signed cert
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
const express = require('express')
|
||||
const fs = require('fs')
|
||||
const bodyParser = require('body-parser')
|
||||
const cors = require('cors')
|
||||
const config = require('../config.json')
|
||||
|
||||
require('./signal')
|
||||
const https = require('https')
|
||||
const WebSocketServer = require('ws').Server
|
||||
|
||||
const app = express()
|
||||
const {port} = config
|
||||
const server = https.createServer({
|
||||
key: fs.readFileSync('key.pem'),
|
||||
cert: fs.readFileSync('cert.pem')
|
||||
}, app)
|
||||
const wss = new WebSocketServer({ server })
|
||||
const {port, ws_port} = config
|
||||
|
||||
app.use(cors())
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
@@ -14,6 +20,107 @@ app.use(bodyParser.json())
|
||||
|
||||
app.use(express.static('./client'))
|
||||
|
||||
app.listen(port, () => {
|
||||
server.listen(port, () => {
|
||||
console.log(`Launching Lil'Streamy on ${port}`)
|
||||
})
|
||||
|
||||
|
||||
|
||||
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];
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user