callback.es.tst 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. callback.tst - Http tests using callbacks
  3. */
  4. if (false) {
  5. const HTTP = tget('TM_HTTP') || "127.0.0.1:8080"
  6. let http: Http = new Http
  7. // Using a read callback
  8. http.setCallback(Http.Read, function (e) {
  9. if (e is HttpDataEvent) {
  10. if (e.eventMask == Http.Read) {
  11. data = http.readString()
  12. }
  13. } else if (e is HttpErrorEvent) {
  14. throw e
  15. } else {
  16. throw "Bad event in http callbac"
  17. }
  18. })
  19. http.get(HTTP + "/big.asp")
  20. http.wait()
  21. // print(http.status)
  22. // Using a write callback
  23. writeCount = 5
  24. http.chunked = true
  25. http.setCallback(Http.Write, function (e) {
  26. if (e is HttpDataEvent) {
  27. // print("MASK " + e.eventMask)
  28. if (e.eventMask & Http.Write) {
  29. // print("WRITE DATA " + writeCount)
  30. if (writeCount-- > 0) {
  31. http.write("WRITE DATA " + writeCount + " \n")
  32. } else {
  33. http.write()
  34. }
  35. }
  36. if (e.eventMask & Http.Read) {
  37. print("READ EVENT ")
  38. }
  39. } else if (e is HttpErrorEvent) {
  40. throw e
  41. } else {
  42. throw "Bad event in http callbac"
  43. }
  44. })
  45. http.post(HTTP + "/f.asp")
  46. http.wait()
  47. // print("CODE " + http.status)
  48. // print("GOT " + http.response.length + " bytes of response")
  49. }