Showing
4 changed files
with
34 additions
and
16 deletions
1 | import express from "express"; | 1 | import express from "express"; |
2 | import socketIo, { Server as IoServer } from "socket.io"; | 2 | import socketIo, { Server as IoServer } from "socket.io"; |
3 | -import { createServer } from "https"; | 3 | +import { createServer as createServerHttps } from "https"; |
4 | +import { createServer as createServerHttp } from "http"; | ||
4 | import { RoomManager } from "./room/RoomManager"; | 5 | import { RoomManager } from "./room/RoomManager"; |
5 | import { Connection } from "./connection/Connection"; | 6 | import { Connection } from "./connection/Connection"; |
6 | import { SocketIoWrapper } from "./connection/SocketWrapper"; | 7 | import { SocketIoWrapper } from "./connection/SocketWrapper"; |
... | @@ -13,21 +14,28 @@ export class Server { | ... | @@ -13,21 +14,28 @@ export class Server { |
13 | constructor(port: number) { | 14 | constructor(port: number) { |
14 | this.port = port; | 15 | this.port = port; |
15 | 16 | ||
17 | + console.log(`Running in ${process.env.NODE_ENV} mode.`); | ||
18 | + | ||
16 | const app = express(); | 19 | const app = express(); |
17 | - const server = createServer( | 20 | + let server; |
18 | - { | 21 | + if (process.env.NODE_ENV === "development") { |
19 | - cert: readFileSync( | 22 | + server = createServerHttp(app); |
20 | - "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" | 23 | + } else { |
21 | - ), | 24 | + server = createServerHttps( |
22 | - key: readFileSync( | 25 | + { |
23 | - "/etc/letsencrypt/live/2020105578.oss2021.tk/privkey.pem" | 26 | + cert: readFileSync( |
24 | - ), | 27 | + "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" |
25 | - ca: readFileSync( | 28 | + ), |
26 | - "/etc/letsencrypt/live/2020105578.oss2021.tk/fullchain.pem" | 29 | + key: readFileSync( |
27 | - ), | 30 | + "/etc/letsencrypt/live/2020105578.oss2021.tk/privkey.pem" |
28 | - }, | 31 | + ), |
29 | - app | 32 | + ca: readFileSync( |
30 | - ); | 33 | + "/etc/letsencrypt/live/2020105578.oss2021.tk/fullchain.pem" |
34 | + ), | ||
35 | + }, | ||
36 | + app | ||
37 | + ); | ||
38 | + } | ||
31 | this.io = new socketIo.Server(server, { | 39 | this.io = new socketIo.Server(server, { |
32 | cors: { | 40 | cors: { |
33 | origin: "*", | 41 | origin: "*", | ... | ... |
1 | import React from "react"; | 1 | import React from "react"; |
2 | import { io } from "socket.io-client"; | 2 | import { io } from "socket.io-client"; |
3 | 3 | ||
4 | -export const socket = io(`https://${window.location.hostname}:3000/`); | 4 | +export const socket = io( |
5 | + `${window.location.protocol}//${window.location.hostname}:3000/` | ||
6 | +); | ||
5 | const SocketContext = React.createContext(socket); | 7 | const SocketContext = React.createContext(socket); |
6 | 8 | ||
7 | export const SocketProvider = SocketContext.Provider; | 9 | export const SocketProvider = SocketContext.Provider; | ... | ... |
-
Please register or login to post a comment