From 2bddb7dfc5c7161ac90af350b412e8f32384568e Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Wed, 18 Nov 2020 17:38:11 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20/devices=20route=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/class/Socket.ts | 18 +++++++++++++++--- server/src/index.ts | 2 ++ server/src/routes/Routes.ts | 6 ++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/server/src/class/Socket.ts b/server/src/class/Socket.ts index cde46f7..0d442fc 100644 --- a/server/src/class/Socket.ts +++ b/server/src/class/Socket.ts @@ -5,6 +5,10 @@ interface database { closeWrite(): void; } +interface server extends SocketIO.Server { + allSockets(): Promise<{}>; +} + interface packet { uuid: string; measurement: number; @@ -12,7 +16,7 @@ interface packet { } export class Socket { - private io!: SocketIO.Server; + private io!: server; constructor( private eventName: string, @@ -50,8 +54,16 @@ export class Socket { }); } - get rooms() { - return this.io.sockets.adapter.rooms; + get rooms(): Array { + let roomsArray: Array = []; + this.io.allSockets().then((msg: any) => { + console.log(msg); + const iterator = msg.entries(); + for (const entry of iterator) { + roomsArray.push(entry[0]); + } + }); + return roomsArray; } closeSocket(): void { diff --git a/server/src/index.ts b/server/src/index.ts index dbd510d..d279587 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,6 +1,7 @@ require('dotenv').config(); import express from 'express'; import chalk from 'chalk'; +var cors = require('cors'); import { Service } from './class/Service'; import { AppRouter } from './AppRouter'; import { Routes } from './routes/Routes'; @@ -11,6 +12,7 @@ const app = express(); const http = require('http').Server(app); const service = new Service(http); +app.use(cors()); app.use(AppRouter.getInstance()); const route = new Routes(app, service); diff --git a/server/src/routes/Routes.ts b/server/src/routes/Routes.ts index f45ed02..f724c5e 100644 --- a/server/src/routes/Routes.ts +++ b/server/src/routes/Routes.ts @@ -11,11 +11,9 @@ export class Routes { private devices(): void { this.app.get('/devices', async (req, res) => { - const data = this.service.socket.rooms; - console.log(data); - const parsedOata = JSON.stringify(data); + const data = await this.service.socket.rooms; res.header('Content-Type', 'application/json'); - res.send(parsedOata); + res.send(JSON.stringify(data)); }); }