import { Server as WebSocketServer } from 'ws';
import { encodeServerState, decodeClientMessage } from './encode';

const INTERVAL = process.env.INTERVAL || 16;

export default function createServer() {
  const server = new WebSocketServer({ port: 8080 });
  const state = [];

  server.on( 'connection', socket => {
    const id = state.length;

    const interval = setInterval(() => {
      try {
        socket.send( encodeServerState( state ), { binary: true } );
       } catch ( error ) {
         console.error( error );
         clearInterval( interval );
       }
    }, INTERVAL );

    socket.on( 'message', message =>
      state[ id ] = decodeClientMessage( message )
    );

    socket.on( 'close', () => {
      clearInterval( interval );
      delete state[ id ];
    });
  });

  return server;
}

import { Server as WebSocketServer } from 'ws';
import { encodeServerState, decodeClientMessage } from './encode';

const INTERVAL = process.env.INTERVAL || 16;

export default function createServer() {
  const server = new WebSocketServer({ port: 8080 });
  const state = [];

  server.on( 'connection', socket => {
    const id = state.length;
    state[ id ] = null;

    const interval = setInterval(() => {
      try {
        socket.send( encodeServerState( state ), { binary: true } );
       } catch ( error ) {
         console.error( error );
         clearInterval( interval );
       }
    }, INTERVAL );

    socket.on( 'message', message =>
      state[ id ] = decodeClientMessage( message )
    );

    socket.on( 'close', () => {
      clearInterval( interval );
      delete state[ id ];
    });
  });

  return server;
}

Add WebSocket client placeholder state.
Add WebSocket client placeholder state.

If two clients connect to the server at the same time, a race condition
may occur where they have the same id. Instead of assigning client ids
on the first client state message, we assign ids on connect.
