#! /usr/bin/env guile # -*-scheme-*- !# ;;; 8sync --- Asynchronous programming for Guile ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen ;;; ;;; This file is part of 8sync. ;;; ;;; 8sync is free software: you can redistribute it and/or modify it ;;; under the terms of the GNU Lesser General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; 8sync is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with 8sync. If not, see . (define-module (8s-server) #:use-module (oop goops) #:use-module (8sync) #:use-module (8sync systems web) #:use-module (8sync systems websocket client) #:use-module (8sync systems websocket server) #:export (main)) (define %server-port 1236) (define-actor () ((*init* sleeper-loop)) (sleep-secs #:init-value 3 #:getter sleeper-sleep-secs)) (define (sleeper-loop actor message) (while (actor-alive? actor) (display "Zzzzzzzz....\n") ;; Sleep for a bit (8sleep (sleeper-sleep-secs actor)))) (define (main . args) (let* ((hive (make-hive)) (sleeper (bootstrap-actor hive )) (suspendable-port-bug? #t) ;; server must send large message to browser (server (bootstrap-actor hive #:port %server-port #:on-ws-client-connect (lambda args (format (current-error-port) "on-ws-client-connection: ~s\n" args)) #:on-ws-client-disconnect (lambda args (format (current-error-port) "on-ws-client-disconnect: ~s\n" args)) #:on-ws-message (lambda (ws id msg) (format (current-error-port) "on-message: ~s: ~s\n" 'ws (if (< (string-length msg) 80) msg (string-append (substring msg 0 77) "..."))) (<- (actor-id ws) 'ws-send id msg) (when suspendable-port-bug? (<- (actor-id ws) 'ws-send id (string-append "Say: " (make-string (expt 2 16) #\A) "\n")))) #:on-ws-open (lambda (ws) (format (current-error-port) "on-open: ~s\n" ws))))) (format (current-error-port) "listening: ~s\n" %server-port) (run-hive hive '()))) ;; HACK: Do not suspend writes to avoid ;; `The connection to ws://localhost:1236/ was interrupted while the page was loading.' (icecat) ;; `could not decode a text frame as utf-8' (ungoogled chromium) ;; (define put-bytevector (@ (ice-9 binary-ports) put-bytevector)) ;; (module-define! (resolve-module '(8sync systems websocket frame)) 'put-bytevector put-bytevector) (main (command-line))