1
- const { Server } = require ( "socket.io" ) ;
1
+ const { Server } = require ( 'socket.io' ) ;
2
+ const express = require ( 'express' ) ;
3
+ const { createServer } = require ( 'http' ) ;
4
+ const cors = require ( 'cors' ) ;
2
5
3
- const io = new Server ( { /* options */ } ) ;
6
+ const app = express ( ) ;
7
+ app . use ( cors ( ) ) ;
4
8
5
- io . on ( "connection" , ( socket ) => {
9
+ app . get ( '/' , ( req , res ) => {
10
+ res . sendFile ( __dirname + '/index.html' ) ;
11
+ // res.send('<h1>Worker up</h1>');
12
+ } ) ;
13
+
14
+ const httpServer = createServer ( app ) ;
15
+ const io = new Server ( httpServer , {
16
+ cors : {
17
+ origin : [ 'https://cnotv-multi-game.netlify.app/' , 'http://localhost:4000/' ] ,
18
+ methods : [ 'GET' , 'POST' ] ,
19
+ } ,
20
+ } ) ;
21
+
22
+ io . on ( 'connection' , ( socket ) => {
6
23
const count = io . engine . clientsCount ;
7
- const uuid = require ( " uuid" ) ;
24
+ const uuid = require ( ' uuid' ) ;
8
25
9
26
io . engine . generateId = ( req ) => {
10
27
return uuid . v4 ( ) ; // must be unique across all Socket.IO servers
11
28
}
12
29
13
30
console . log ( `New connection: ${ socket . id } - Total clients: ${ count } ` ) ;
14
31
15
- io . engine . on ( " connection_error" , ( err ) => {
32
+ io . engine . on ( ' connection_error' , ( err ) => {
16
33
console . log ( err ) ;
17
34
} ) ;
18
35
} ) ;
19
36
20
-
21
- io . listen ( 3000 ) ;
37
+ httpServer . listen ( 3000 , ( ) => {
38
+ console . log ( 'Server ready' ) ;
39
+ } ) ;
0 commit comments