test-zmq-weather-server.nut 914 B

12345678910111213141516171819202122232425262728293031323334
  1. //
  2. // Weather update server
  3. // Binds PUB socket to tcp://*:5556
  4. // Publishes random weather updates
  5. //
  6. // Author: Robert G. Jakabosky <[email protected]>
  7. //
  8. // Prepare our context and publisher
  9. local context = zmq_ctx();
  10. print(__LINE__, context);
  11. local publisher = context.socket(zmq_socket.PUB);
  12. print(__LINE__, publisher);
  13. print(__LINE__, publisher.bind("tcp://*:5556"));
  14. print(__LINE__, publisher.bind("ipc://weather.ipc"));
  15. // Initialize random number generator
  16. math.srand(os.time());
  17. while (1) {
  18. // Get values that will fool the boss
  19. local zipcode, temperature, relhumidity;
  20. zipcode = math.random(0, 99999);
  21. //zipcode = 10001;
  22. temperature = math.random(-80, 135);
  23. relhumidity = math.random(10, 60);
  24. // Send message to all subscribers
  25. //print(__LINE__,
  26. publisher.send(format("%05d %d %d", zipcode, temperature, relhumidity)); //);
  27. //os.sleep(0.0001);
  28. }
  29. publisher.close();
  30. context.destroy();