X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=demos%2Fwebsocket%2F8s-client.scm;fp=demos%2Fwebsocket%2F8s-client.scm;h=09fe137f5ecbba80021996de34377938416e1801;hb=9f0c24e3e0f610303b9af8ff979c8e4408713cc8;hp=0000000000000000000000000000000000000000;hpb=183b4f02e68279d4984e79b79e06bfcf1861fcbf;p=8sync.git diff --git a/demos/websocket/8s-client.scm b/demos/websocket/8s-client.scm new file mode 100755 index 0000000..09fe137 --- /dev/null +++ b/demos/websocket/8s-client.scm @@ -0,0 +1,61 @@ +#! /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-client) + #:use-module (oop goops) + #:use-module (8sync) + #:use-module (8sync systems websocket client) + + #:export (main)) + +(define %default-server "ws://localhost: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 )) + (websocket-id (bootstrap-actor hive + #:url %default-server ;; toggle open with url/with message + #:on-close (lambda (ws) + (format (current-error-port) "on-close: ~s\n" ws)) + #:on-error (lambda (ws e) + (format (current-error-port) "on-error: ~s:~s\n" ws e)) + #:on-message (lambda (ws msg) + (format (current-error-port) "on-message: ~s:~s\n" ws msg)) + #:on-open (lambda (ws) + (format (current-error-port) "on-open: ~s\n" ws) + (websocket-send ws 'message "Hello, Web Socket!")))) + (websocket ((@@ (8sync actors) hive-resolve-local-actor) hive websocket-id))) + (if (.url websocket) + (run-hive hive '()) + (run-hive hive (list (bootstrap-message hive websocket-id 'open %default-server)))))) + +(main (command-line))