server.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const io = require('socket.io')(8123, {
  2. pingInterval: 30005,
  3. pingTimeout: 5000,
  4. upgradeTimeout: 3000,
  5. allowUpgrades: true,
  6. cookie: false,
  7. serveClient: true
  8. });
  9. // App Code starts here
  10. console.log('Starting Socket.IO demo server');
  11. io.on('connection', (socket) => {
  12. console.log('[' + (new Date()).toUTCString() + '] game connecting');
  13. socket.on('KnockKnock', (data) => {
  14. console.log('[' + (new Date()).toUTCString() + '] game knocking... Answering "Who\'s there?"...');
  15. socket.emit('WhosThere');
  16. });
  17. socket.on('ItsMe', async (data) => {
  18. console.log('[' + (new Date()).toUTCString() + '] received game introduction. Welcoming the guest...');
  19. socket.emit('Welcome', 'Hi customer using unity' + data.version + ', this is the backend microservice. Thanks for buying our asset. (No data is stored on our server)');
  20. socket.emit('TechData', {
  21. podName: 'Local Test-Server',
  22. timestamp: (new Date()).toUTCString()
  23. });
  24. });
  25. socket.on('SendNumbers', async (data) => {
  26. console.log('[' + (new Date()).toUTCString() + '] Client is asking for random number array');
  27. socket.emit('RandomNumbers', [ Math.ceil((Math.random() * 100)), Math.ceil((Math.random() * 100)), Math.ceil((Math.random() * 100)) ]);
  28. });
  29. socket.on('Goodbye', async (data) => {
  30. console.log('[' + (new Date()).toUTCString() + '] Client said "' + data + '" - The server will now disconnect the client.');
  31. socket.disconnect(true);
  32. });
  33. socket.on('disconnect', (data) => {
  34. console.log('[' + (new Date()).toUTCString() + '] Bye, client ' + socket.id);
  35. });
  36. socket.on('PING', async (data) => {
  37. console.log('[' + (new Date()).toUTCString() + '] incoming PING #' + data + ' from ' + socket.id + ' answering PONG with some jitter...');
  38. await sleep(Math.random() * 2000);
  39. socket.emit('PONG', data);
  40. });
  41. });