Skip to content

Commit

Permalink
Merge pull request #148 from mind-ar/fix-diagrama-socket
Browse files Browse the repository at this point in the history
Diagrama: permitir conexiones desde cualquier origen
  • Loading branch information
PalumboN committed Jun 5, 2024
2 parents 85e45ec + 2380422 commit 557709b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 1 addition & 2 deletions public/diagram/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
<script type="module">
import { io } from "./lib/socket.io.esm.min.js";

const { port } = window.location
const socket = io(`http://localhost:${port}`);
const socket = io();
socket.on("connect", function () {
console.log("conectado!");
});
Expand Down
6 changes: 4 additions & 2 deletions src/commands/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Options = {
project: string
skipValidations: boolean,
darkMode: boolean,
host: string,
port: string,
skipDiagram: boolean,
}
Expand Down Expand Up @@ -282,9 +283,10 @@ export async function initializeClient(options: Options, repl: Interface, interp
cors({ allowedHeaders: '*' }),
express.static(publicPath('diagram'), { maxAge: '1d' }),
)
server.listen(parseInt(options.port), 'localhost')
const host = options.host
server.listen(parseInt(options.port), host)
server.addListener('listening', () => {
logger.info(successDescription('Dynamic diagram available at: ' + bold(`http://localhost:${options.port}`)))
logger.info(successDescription('Dynamic diagram available at: ' + bold(`http://${host}:${options.port}`)))
repl.prompt()
})

Expand Down
17 changes: 11 additions & 6 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ type Options = {
project: string
assets: string
skipValidations: boolean
port?: string
host: string,
port: string
game: boolean,
startDiagram: boolean
}

let timer = 0

const DEFAULT_PORT = '4200'
const DEFAULT_HOST = 'localhost'

type DynamicDiagramClient = {
onReload: () => void,
Expand Down Expand Up @@ -120,7 +122,7 @@ export const getGameInterpreter = (environment: Environment, io: Server): Interp
return interpreter
}

export const initializeGameClient = ({ project, assets, port, game }: Options): Server | undefined => {
export const initializeGameClient = ({ project, assets, host, port, game }: Options): Server | undefined => {
if (!game) return undefined

const app = express()
Expand All @@ -137,10 +139,11 @@ export const initializeGameClient = ({ project, assets, port, game }: Options):
app.use(cors({ allowedHeaders: '*' }), express.static(soundsFolder, { maxAge: '1d' }))
}

const currentHost = gameHost(host!)
const currentPort = gamePort(port!)
server.listen(parseInt(currentPort), 'localhost')
server.listen(parseInt(currentPort), currentHost)

logger.info(successDescription('Game available at: ' + bold(`http://localhost:${currentPort}`)))
logger.info(successDescription('Game available at: ' + bold(`http://${currentHost}:${currentPort}`)))
server.listen(currentPort)
return io
}
Expand Down Expand Up @@ -170,10 +173,11 @@ export async function initializeDynamicDiagram(programPackage: Package, options:
cors({ allowedHeaders: '*' }),
express.static(publicPath('diagram'), { maxAge: '1d' }),
)
const currentHost = gameHost(options.host!)
const currentPort = dynamicDiagramPort(options.port!)
server.listen(parseInt(currentPort), 'localhost')
server.listen(parseInt(currentPort), currentHost)
server.addListener('listening', () => {
logger.info(successDescription('Dynamic diagram available at: ' + bold(`http://localhost:${currentPort}`)))
logger.info(successDescription('Dynamic diagram available at: ' + bold(`http://${currentHost}:${currentPort}`)))
})

return {
Expand Down Expand Up @@ -283,6 +287,7 @@ export const buildEnvironmentForProgram = async ({ project, skipValidations, gam
export const runner = (game: boolean): string => game ? 'as a game' : 'as a program'

export const gamePort = (port: string): string => port ?? DEFAULT_PORT
export const gameHost = (host: string): string => host ?? DEFAULT_HOST

export const dynamicDiagramPort = (port: string): string => `${+gamePort(port) + 1}`

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ program.command('run')
.option('-p, --project <path>', 'path to project', process.cwd())
.option('-a, --assets [path]', 'path relative to project for game assets. By default, it takes the assets definition from package.json.', 'assets')
.option('--skipValidations', 'skip code validation', false)
.option('--host [host]', 'host to run (bind) the server', 'localhost')
.option('--port [port]', 'port to run the server', '3000')
.option('-g, --game', 'sets the program as a game', false)
.option('-v, --verbose', 'print debugging information', false)
Expand All @@ -46,6 +47,7 @@ program.command('repl')
.option('--skipValidations', 'skip code validation', false)
.option('--darkMode', 'dark mode', false)
.option('--skipDiagram', 'avoid starting the server for the dynamic diagram', false)
.option('--host [host]', 'host to run (bind) the server', 'localhost')
.option('--port [port]', 'port to run the server', '3000')
.option('-v, --verbose', 'print debugging information', false)
.action(repl)
Expand Down

0 comments on commit 557709b

Please sign in to comment.