run-server.scm 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. (use-modules ((tcp-server) #:prefix server:)
  2. (ice-9 textual-ports)
  3. (json))
  4. (define (json-echo-message-handler client-connection scm-native)
  5. (let ([in-out-sock (car client-connection)])
  6. (display (simple-format #f "RECEIVED JSON: ~s, which is: ~s\n"
  7. scm-native
  8. (scm->json-string scm-native)))
  9. (scm->json scm-native in-out-sock)
  10. (force-output in-out-sock)
  11. (display (simple-format #f "SENT JSON: ~s, which is: ~s\n"
  12. scm-native
  13. (scm->json-string scm-native)))))
  14. (define json-echo-protocol
  15. (server:make-server-protocol
  16. #:port-reader json->scm
  17. #:message-handler json-echo-message-handler
  18. #:eof-handler server:shutdown-client-connection))
  19. ;; JSON ECHO SERVER
  20. (define server-sock
  21. (server:run-server 12345
  22. #:protocol json-echo-protocol))
  23. ;; To run this you need to add the containing directory to the load path.
  24. ;; (add-to-load-path "YOUR DIRECTORY HERE")