(
runtimeScene: gdjs.RuntimeScene,
lobbyId: string
)
| 502 | }; |
| 503 | |
| 504 | const handleJoinLobbyEvent = function ( |
| 505 | runtimeScene: gdjs.RuntimeScene, |
| 506 | lobbyId: string |
| 507 | ) { |
| 508 | if (_connectionId) { |
| 509 | logger.info('Already connected to a lobby.'); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (_websocket) { |
| 514 | logger.warn('Already connected to a lobby. Closing the previous one.'); |
| 515 | _websocket.close(); |
| 516 | _connectionId = null; |
| 517 | playerNumber = null; |
| 518 | hostPeerId = null; |
| 519 | _lobbyId = null; |
| 520 | _websocket = null; |
| 521 | } |
| 522 | |
| 523 | const gameId = gdjs.projectData.properties.projectUuid; |
| 524 | const playerId = gdjs.playerAuthentication.getUserId(); |
| 525 | const playerToken = gdjs.playerAuthentication.getUserToken(); |
| 526 | if (!gameId) { |
| 527 | logger.error('Cannot open lobbies if the project has no ID.'); |
| 528 | return; |
| 529 | } |
| 530 | if (!playerId || !playerToken) { |
| 531 | logger.warn('Cannot open lobbies if the player is not connected.'); |
| 532 | return; |
| 533 | } |
| 534 | const wsPlayApi = isUsingGDevelopDevelopmentEnvironment |
| 535 | ? 'wss://api-ws-dev.gdevelop.io/play' |
| 536 | : 'wss://api-ws.gdevelop.io/play'; |
| 537 | |
| 538 | const wsUrl = new URL(wsPlayApi); |
| 539 | wsUrl.searchParams.set('gameId', gameId); |
| 540 | wsUrl.searchParams.set('lobbyId', lobbyId); |
| 541 | wsUrl.searchParams.set('playerId', playerId); |
| 542 | wsUrl.searchParams.set('connectionType', 'lobby'); |
| 543 | wsUrl.searchParams.set('playerGameToken', playerToken); |
| 544 | _websocket = new WebSocket(wsUrl.toString()); |
| 545 | _websocket.onopen = () => { |
| 546 | logger.info('Connected to the lobby.'); |
| 547 | // Register a heartbeat to keep the connection alive. |
| 548 | _websocketHeartbeatIntervalFunction = setInterval(() => { |
| 549 | if (_websocket) { |
| 550 | _websocket.send( |
| 551 | JSON.stringify({ |
| 552 | action: 'heartbeat', |
| 553 | connectionType: 'lobby', |
| 554 | }) |
| 555 | ); |
| 556 | } |
| 557 | }, DEFAULT_WEBSOCKET_HEARTBEAT_INTERVAL); |
| 558 | |
| 559 | // When socket is open, ask for the connectionId and send more session info, so that we can inform the lobbies window. |
| 560 | if (_websocket) { |
| 561 | _websocket.send(JSON.stringify({ action: 'getConnectionId' })); |
no test coverage detected