hello-client.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const ipc=require('../../../node-ipc');
  2. /***************************************\
  3. *
  4. * UDP Client is really a UDP server
  5. *
  6. * Dedicated UDP sockets on the same
  7. * machine can not be bound to in the
  8. * traditional client/server method
  9. *
  10. * Every UDP socket is it's own UDP server
  11. * And so must have a unique port on its
  12. * machine, unlike TCP or Unix Sockts
  13. * which can share on the same machine.
  14. *
  15. * Since there is no open client server
  16. * relationship, you should start world
  17. * first and then hello.
  18. *
  19. * *************************************/
  20. ipc.config.id = 'hello';
  21. ipc.config.retry= 1500;
  22. ipc.serveNet(
  23. 8001, //we set the port here because the world server is already using the default of 8000. So we can not bind to 8000 while world is using it.
  24. 'udp4',
  25. function(){
  26. ipc.server.on(
  27. 'message',
  28. function(data){
  29. ipc.log('got Data');
  30. ipc.log('got a message from ', data.id ,' : ', data.message);
  31. }
  32. );
  33. ipc.server.emit(
  34. {
  35. address : 'localhost',
  36. port : ipc.config.networkPort
  37. },
  38. 'message',
  39. {
  40. id : ipc.config.id,
  41. message : 'Hello'
  42. }
  43. );
  44. }
  45. );
  46. ipc.server.start();