websocket: Initial <websocket> client actor support.
[8sync.git] / demos / websocket / ws-server.js
1 #! /usr/bin/env node
2
3 /// 8sync --- Asynchronous programming for Guile
4 /// Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ///
6 /// This file is part of 8sync.
7 ///
8 /// 8sync is free software: you can redistribute it and/or modify it
9 /// under the terms of the GNU Lesser General Public License as
10 /// published by the Free Software Foundation, either version 3 of the
11 /// License, or (at your option) any later version.
12 ///
13 /// 8sync is distributed in the hope that it will be useful,
14 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 /// GNU Lesser General Public License for more details.
17 ///
18 /// You should have received a copy of the GNU Lesser General Public
19 /// License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
20
21 var server_port = 1236;
22
23 // npm install -g ws
24 function main () {
25   var wss = new require ('ws').Server ({port:server_port});
26   console.log ('listening: %s', server_port);
27   wss.on ('connection', function (ws) {
28     ws.onopen = function () {
29       console.log ('ws.onopen');
30       // process.nextTick (function () {
31       //   ws.send ('Hello Web Socket');
32       //   ws.close ();
33       // });
34     }
35     ws.onerror = function (e) {
36       console.log ('ws.onerror: e=%s', '' + e);
37     }
38     ws.onclose = function () {
39       console.log ('ws.onclose');
40     }
41     ws.onmessage = function (event) {
42       var msg = event.data;
43       console.log ('ws.onmessage: msg=%s', msg);
44     };
45   });
46 }
47
48 main ();