Showing
4 changed files
with
21 additions
and
3 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,8 +14,14 @@ export class Server { | ... | @@ -13,8 +14,14 @@ 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; |
21 | + if (process.env.NODE_ENV === "development") { | ||
22 | + server = createServerHttp(app); | ||
23 | + } else { | ||
24 | + server = createServerHttps( | ||
18 | { | 25 | { |
19 | cert: readFileSync( | 26 | cert: readFileSync( |
20 | "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" | 27 | "/etc/letsencrypt/live/2020105578.oss2021.tk/cert.pem" |
... | @@ -28,6 +35,7 @@ export class Server { | ... | @@ -28,6 +35,7 @@ export class Server { |
28 | }, | 35 | }, |
29 | app | 36 | app |
30 | ); | 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