Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpijnacker committed May 13, 2020
1 parent 6235e95 commit 773d89b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
13 changes: 13 additions & 0 deletions src/server/Session.ts
@@ -0,0 +1,13 @@
import { Socket } from 'socket.io';

export interface Client {
id: string;
address: string;
name: string;
sessionId: string;
socket: Socket;
}

export default class Session extends Set<Client> {
clients: Client[]
}
22 changes: 22 additions & 0 deletions src/server/SessionManager.ts
@@ -0,0 +1,22 @@
import Session from './Session';

export default class SessionManager {
// sessionId -> Session
static sessions = new Map<string, Session>();

static initializeSession(sessionId: string) {
let session = SessionManager.sessions.get(sessionId);
if (!session) {
session = new Session();
SessionManager.sessions.set(sessionId, session);
}
return session;
}

static cleanupSession(sessionId: string) {
const session = SessionManager.sessions.get(sessionId);
if (session && session.size === 0) {
SessionManager.sessions.delete(sessionId);
}
}
}
60 changes: 16 additions & 44 deletions src/server/index.ts
@@ -1,6 +1,9 @@
// import express from 'express';
import http from 'http';
import socketio, { Socket } from 'socket.io';

import Session, { Client } from './Session';
import SessionManager from './SessionManager';
import UdpEchoServer from './UdpEchoServer';

const echoServer = new UdpEchoServer();
Expand All @@ -14,55 +17,24 @@ server.listen(3000, () => {
console.log('listening on *:3000');
});

interface Client {
id: string;
address: string;
name: string;
sessionId: string;
socket: Socket;
}

class Session extends Set<Client> {

}

// sessionId -> Session
const sessions = new Map<string, Session>();

function initializeSession(sessionId: string) {
let session = sessions.get(sessionId);
if (!session) {
session = new Session();
sessions.set(sessionId, session);
}
return session;
}

function cleanupSession(sessionId: string) {
const session = sessions.get(sessionId);
if (session && session.size === 0) {
sessions.delete(sessionId);
}
}

io.on('connect', (socket) => {
console.log('socket connected');

// session this client is connected to
let session: Session;
const client: Client = {
id: socket.id,
address: undefined,
name: undefined,
sessionId: undefined,
socket,
};
// clients connected to the same session
let otherClients: Set<Client>;

socket.on('disconnect', () => {
if (client.name) {
console.log(`disconnected ${client.name}`);
otherClients.delete(client);
otherClients.forEach((c) => {
session.delete(client);
session.forEach((c) => {
c.socket.emit('user left', { id: client.id, name: client.name });
c.socket.emit('stop sending', {
id: client.id,
Expand All @@ -74,27 +46,27 @@ io.on('connect', (socket) => {
address: client.address,
});
});
console.log(`#${otherClients.size} left`);
cleanupSession(client.sessionId);
console.log(`#${session.size} left`);
SessionManager.cleanupSession(client.sessionId);
}
});

socket.on('identify', ({ name, address, sessionId }, callback) => {
console.log(`identified ${name} on ${address} for session ${sessionId}`);
otherClients = initializeSession(sessionId);
otherClients.forEach((c) =>
session = SessionManager.initializeSession(sessionId);
session.forEach((c) =>
c.socket.emit('user joined', { id: client.id, name })
);
const currentUsers = [...otherClients.values()].map((c) => ({
const currentUsers = [...session.values()].map((c) => ({
id: c.id,
name: c.name,
}));
callback(currentUsers);
client.address = address;
client.name = name;
client.sessionId = sessionId;
otherClients.add(client);
console.log(`#${otherClients.size} connected`);
session.add(client);
console.log(`#${session.size} connected`);
});

socket.on('chat message', (msg) => {
Expand All @@ -103,7 +75,7 @@ io.on('connect', (socket) => {
});

socket.on('start streaming', () => {
otherClients.forEach((other) => {
session.forEach((other) => {
if (other !== client) {
client.socket.emit(
'start receiving',
Expand Down Expand Up @@ -134,7 +106,7 @@ io.on('connect', (socket) => {
});

socket.on('mute microphone', ({ isMuted }) => {
otherClients.forEach((other) => {
session.forEach((other) => {
if (other !== client) {
if (isMuted) {
client.socket.emit('stop sending', {
Expand Down

0 comments on commit 773d89b

Please sign in to comment.