Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. var woopsa = require('../index');
  2. var express = require('express');
  3.  
  4. var app = express();
  5.  
  6. app.use('/public', express.static('public'));
  7. app.listen(80, function (){
  8. console.log("Express app is listening on port 80");
  9. });
  10.  
  11. // Create a test object Weather Station
  12. var weatherStation = {
  13. Temperature: 24.2,
  14. IsRaining: false,
  15. Sensitivity: 0.5,
  16. Altitude: 430,
  17. City: "Geneva",
  18. Time: new Date(),
  19. GetWeatherAtDate: function (date){
  20. if ( date.getDay() === 1 )
  21. return "rainy";
  22. else
  23. return "sunny";
  24. },
  25. Thermostat: {
  26. SetPoint: 24.0
  27. }
  28. }
  29.  
  30. var server = new woopsa.Server(weatherStation, {
  31. port: 80,
  32. listenCallback: function (err){
  33. if ( !err ){
  34. console.log("Woopsa server listening on http://localhost:%d", server.options.port);
  35. console.log("Some examples of what you can do directly from your browser:");
  36. console.log(" * View the object hierarchy of the root object:");
  37. console.log(" http://localhost:%d%smeta/", server.options.port, server.options.pathPrefix);
  38. console.log(" * Read the value of a property:");
  39. console.log(" http://localhost:%d%sread/Temperature", server.options.port, server.options.pathPrefix);
  40. }else{
  41. console.log("Error");
  42. }
  43. },
  44. // The typer function allows us to specify the actual
  45. // Woopsa type for a WoopsaElement when we can't guess (infer)
  46. // it in JavaScript. For example, all function parameters
  47. // and return types are set to Text when using the reflector.
  48. // By returning DateTime for /GetWeatherAtDate/date, we
  49. // allow the library to directly send us a JavaScript
  50. // Date object.
  51. typer: function (path, inferredType){
  52. if ( path === "/GetWeatherAtDate/date" )
  53. return "DateTime";
  54. else
  55. return inferredType;
  56. },
  57. checkAuthenticate: function (username, password) {
  58. if (username === "test" && password === "test")
  59. return true;
  60. else
  61. return false;
  62. },
  63. expressApp: app
  64. });
  65.  
  66.  
  67.  
  68. // server.httpServer.on('error', function (err){
  69. // console.log("Error: Could not start Woopsa Server. Most likely because an application is already listening on port %d.", server.options.port);
  70. // console.log("Known culprits:");
  71. // console.log(" - On Windows 10, IIS is on by default on some configurations.");
  72. // console.log(" - Skype");
  73. // console.log(" - Apache, nginx, etc.");
  74. // console.log("%s", err);
  75. // });
  76.  
  77. // Make the temperature of our WeatherStation fluctuate randomly
  78. setInterval(function (){
  79. weatherStation.Temperature += Math.random() - 0.5;
  80. }, 20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement