|
1 | | -import cors from 'cors'; |
2 | | -import SocketIOClient, { Socket } from 'socket.io'; |
| 1 | +import express from 'express'; |
| 2 | +import path from 'path'; |
3 | 3 |
|
4 | | -import { createApp } from './express.server'; |
5 | | -import { envConstants, connectToDB } from 'core'; |
6 | | -import { api } from './api'; |
7 | | -import { processInputMessage, InputMessageTypes, processOutputMessageCollection, Action, SocketInfo } from './messages'; |
| 4 | +import { createApp, createSocketServer } from 'core/servers'; |
| 5 | +import { connectToDB } from 'core/db'; |
| 6 | +import { envConstants } from 'core/constants'; |
| 7 | +import { api } from 'pods/room'; |
8 | 8 |
|
9 | 9 | const app = createApp(); |
10 | | - |
11 | | -let http = require('http').Server(app); |
12 | | -// set up socket.io and bind it to our |
13 | | -// http server. |
14 | | -let io: SocketIOClient.Socket = require('socket.io')(http); |
15 | | - |
16 | | -//options for cors midddleware |
17 | | -const options: cors.CorsOptions = { |
18 | | - allowedHeaders: [ |
19 | | - 'Origin', |
20 | | - 'X-Requested-With', |
21 | | - 'Content-Type', |
22 | | - 'Accept', |
23 | | - 'X-Access-Token', |
24 | | - ], |
25 | | - credentials: true, |
26 | | - methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE', |
27 | | - // IMPORTANT LIMIT HERE YOUR CLIENT APPS DOMAINS |
28 | | - origin: '*', |
29 | | - preflightContinue: false, |
30 | | -}; |
31 | | - |
32 | | -app.use(cors(options)); |
33 | 10 | app.use(envConstants.API_URL, api); |
34 | 11 |
|
35 | | -app.listen(envConstants.PORT, async () => { |
| 12 | +const staticFilesPath = path.resolve(__dirname, envConstants.STATIC_FILES_PATH); |
| 13 | +app.use('/', express.static(staticFilesPath)); |
| 14 | + |
| 15 | +const appServer = app.listen(envConstants.PORT, async () => { |
36 | 16 | if(!envConstants.isMockRepository && envConstants.MONGODB_URI) { |
37 | 17 | await connectToDB(envConstants.MONGODB_URI); |
38 | 18 | } |
39 | 19 | console.log(`Using ${envConstants.isMockRepository ? 'Mock' : 'DataBase'} for session storage`) |
40 | 20 | console.log(`Server ready at http://localhost:${envConstants.PORT}${envConstants.API_URL}`); |
41 | 21 | }); |
42 | 22 |
|
43 | | -// whenever a user connects on port 3000 via |
44 | | -// a websocket, log that a user has connected |
45 | | -io.on('connection', async function (socket: Socket) { |
46 | | - // WATCH OUT !! Reconnect is not implemented |
47 | | - // In the connection input processing, we should |
48 | | - // check if connectionId matches with userId and RoomId |
49 | | - // if not reject, if it's accepted send connection |
50 | | - // reestablished |
51 | | - const { room, trainertoken } = socket.handshake.query; |
52 | | - console.log(`room request: ${room}`); |
53 | | - console.log(`trainer token: ${trainertoken}`); |
54 | | - console.log('*** Session ID:', socket.conn.id); |
55 | | - |
56 | | - let outputMessageCollection: Action[] = []; |
57 | | - const socketInfo: SocketInfo = { |
58 | | - socket: socket, |
59 | | - io, |
60 | | - connectionId: socket.conn.id, |
61 | | - }; |
62 | | - |
63 | | - // TODO encapuslate this to processInputMessage |
64 | | - if (trainertoken) { |
65 | | - outputMessageCollection = await processInputMessage(socketInfo, { |
66 | | - type: InputMessageTypes.ESTABLISH_CONNECTION_TRAINER, |
67 | | - payload: { |
68 | | - room, |
69 | | - trainertoken, |
70 | | - }, |
71 | | - }); |
72 | | - } else { |
73 | | - outputMessageCollection = await processInputMessage(socketInfo, { |
74 | | - type: InputMessageTypes.ESTABLISH_CONNECTION_STUDENT, |
75 | | - payload: { |
76 | | - room, |
77 | | - }, |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - processOutputMessageCollection(socketInfo, outputMessageCollection); |
82 | | - |
83 | | - socket.on('message', async function (message: any) { |
84 | | - console.log(message); |
85 | | - if (message && message.type) { |
86 | | - const outputMessageCollection: Action[] = await processInputMessage( |
87 | | - socketInfo, |
88 | | - message |
89 | | - ); |
| 23 | +createSocketServer(appServer); |
90 | 24 |
|
91 | | - await processOutputMessageCollection(socketInfo, outputMessageCollection); |
92 | | - } |
93 | | - }); |
94 | | -}); |
95 | | - |
96 | | -const server = http.listen(3001, function () { |
97 | | - console.log('listening on *:3001'); |
98 | | -}); |
0 commit comments