Developer Documentation

OdinConnectionStateChangedEvent

type OdinConnectionStateChangedEvent = (event: IOdinEvent<IOdinConnectionStateChangedEventPayload>)

Discussion

The OdinConnectionStateChangedEvent event is emitted on OdinClient and OdinRoom instances when the internal connection state of the underlying main/room stream is updated. You can use the event to determine when your connection is closed unexpectedly (e.g. timeout or server shutdown).

Events are inheriting from IOdinEvent and provide an object described by IOdinConnectionStateChangedEventPayload in the payload property of the event.

Event Scopes

  • IOdinClientEvents
  • IOdinRoomEvents

Example

OdinConnectionStateChangedEvent Example
import { OdinClient } from '@4players/odin';

const startOdin = async function (token: string) {
  // Authenticate using a token obtained externally and spawn a room instance
  const odinRoom = await OdinClient.initRoom(token);

  // Listen for connection state change events of the main stream
  odinRoom.addEventListener('ConnectionStateChanged', (event) => {
    console.log(`Room connection state changed from ${event.payload.oldState} to ${event.payload.newState}`);
  });

  // Join the room
  odinRoom.join();
};

startOdin('__YOUR TOKEN__').then(() => {
  console.log('Started ODIN');
});

Parameters

NameTypeDescription
eventIOdinEvent<IOdinConnectionStateChangedEventPayload>