|
| 1 | +const express = require('express'); |
| 2 | +const app = express(); |
| 3 | +const http = require('http').createServer(app); |
| 4 | +const io = require('socket.io')(http); |
| 5 | +const ip = require('ip'); |
| 6 | + |
| 7 | +//----------------------------------------------------------------------------// |
| 8 | + |
| 9 | +const MODE_PRODUCTION = ( process.env.MODE === 'production' ); |
| 10 | + |
| 11 | +//----------------------------------------------------------------------------// |
| 12 | + |
| 13 | +let ipAddress = 'localhost'; // will works only to the local host |
| 14 | +try { |
| 15 | + // will enable to the server to be accessed from the network |
| 16 | + ipAddress = ip.address(); |
| 17 | +} catch( err ){ |
| 18 | + console.err( err ); |
| 19 | +} |
| 20 | + |
| 21 | +const SERVER_PORT = 3000; |
| 22 | + |
| 23 | +//----------------------------------------------------------------------------// |
| 24 | +// CORS configs - allows access from the other machines beyond the localhost |
| 25 | + |
| 26 | +app.use( ( req, res, next ) => { |
| 27 | + res.header('Access-Control-Allow-Origin', '*'); |
| 28 | + res.header('Access-Control-Allow-Headers', 'X-Requested-With'); |
| 29 | + res.header('Access-Control-Allow-Headers', 'Content-Type'); |
| 30 | + res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); |
| 31 | + next(); |
| 32 | +}); |
| 33 | + |
| 34 | +app.options( ( req, res ) => { |
| 35 | + res.header('Access-Control-Allow-Origin', '*'); |
| 36 | + res.header('Access-Control-Allow-Headers', 'X-Requested-With'); |
| 37 | + res.header('Access-Control-Allow-Headers', 'Content-Type'); |
| 38 | + res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); |
| 39 | + res.send(200); |
| 40 | +}); |
| 41 | + |
| 42 | +//----------------------------------------------------------------------------// |
| 43 | + |
| 44 | +app.use( express.static( `${__dirname}/public` ) ); |
| 45 | + |
| 46 | +app.get( '/', ( req, res ) => { |
| 47 | + res.sendFile( `${__dirname}/public/index.html` ); |
| 48 | +}); |
| 49 | + |
| 50 | +//----------------------------------------------------------------------------// |
| 51 | +// Socket IO |
| 52 | + |
| 53 | +const IO_EVENT_CONNECTION = 'connection'; |
| 54 | +const IO_EVENT_MESSAGE = 'message'; |
| 55 | +const IO_EVENT_DISCONNECT = 'disconnect'; |
| 56 | + |
| 57 | +let connectionsCounter = 1; |
| 58 | + |
| 59 | +const logConnections = () => { |
| 60 | + if( !MODE_PRODUCTION ) { |
| 61 | + console.log( `Socket IO connections: ${connectionsCounter}` ); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +const logMessage = message => { |
| 66 | + if( !MODE_PRODUCTION ) { |
| 67 | + console.log( |
| 68 | + 'Socket IO server received the message: ', |
| 69 | + JSON.stringify( message ) |
| 70 | + ); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +io.on( IO_EVENT_CONNECTION, socket => { |
| 75 | + logConnections(); |
| 76 | + connectionsCounter++; |
| 77 | + |
| 78 | + socket.on( IO_EVENT_MESSAGE, message => { |
| 79 | + logMessage( message ); |
| 80 | + |
| 81 | + // return the message to the sender |
| 82 | + socket.emit( IO_EVENT_MESSAGE, message ); |
| 83 | + |
| 84 | + // send the message to the others listeners |
| 85 | + socket.broadcast.emit( IO_EVENT_MESSAGE, message ); |
| 86 | + }); |
| 87 | + |
| 88 | + socket.on( IO_EVENT_DISCONNECT, () => { |
| 89 | + connectionsCounter--; |
| 90 | + logConnections(); |
| 91 | + }) |
| 92 | +}); |
| 93 | + |
| 94 | +//----------------------------------------------------------------------------// |
| 95 | +// start the server |
| 96 | + |
| 97 | +http.listen( SERVER_PORT, ipAddress, () => { |
| 98 | + let message = [ |
| 99 | + '\n', |
| 100 | + `Socket IO server is running at ${ipAddress}:${SERVER_PORT}`, |
| 101 | + '\n\n' |
| 102 | + ]; |
| 103 | + |
| 104 | + if( !MODE_PRODUCTION ) { |
| 105 | + message.push( |
| 106 | + 'Please remember to update the .env file, and if needed restart your dev. environment.\n\n' |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + console.log( ...message ) |
| 111 | +}); |
0 commit comments