syndicate-js/packages/broker/src/index.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-11-13 21:27:26 +00:00
"use strict";
const UI = require("@syndicate-lang/driver-browser-ui");
// @jsx UI.html
// @jsxFrag UI.htmlFragment
const Http = activate require("@syndicate-lang/driver-http-node");
const Tcp = activate require("@syndicate-lang/driver-tcp-node");
2018-11-15 23:52:48 +00:00
import { Decoder, Bytes } from "@syndicate-lang/core";
2018-11-13 21:27:26 +00:00
const server = Http.HttpServer(null, 8000);
spawn named 'serverLogger' {
on asserted Http.Request(_, server, $method, $path, $query, $req) {
console.log(method, path.toJS(), query.toJS());
}
2018-11-15 11:00:30 +00:00
on asserted Http.WebSocket(_, server, $path, $query) {
console.log(path.toJS(), query.toJS());
}
2018-11-13 21:27:26 +00:00
}
spawn named 'rootServer' {
during Http.Request($reqId, server, 'get', [], _, _) {
assert :snapshot Http.Response(
reqId, 200, "OK", {"Content-type": "text/html"},
'<!DOCTYPE html>' + UI.htmlToString(
<div>
<p>Hello</p>
</div>
));
}
}
spawn named 'websocketListener' {
during Http.WebSocket($reqId, server, ['broker'], _) spawn named ['wsConnection', reqId] {
on message Http.DataIn(reqId, $message) {
2018-11-15 23:52:48 +00:00
console.log('got', reqId, new Decoder(message).next());
send Http.DataOut(reqId, message);
2018-11-13 21:27:26 +00:00
}
2018-11-15 23:52:48 +00:00
stop on message Http.DataIn(reqId, Bytes.from("quit"));
2018-11-13 21:27:26 +00:00
}
}
spawn named 'tcpListener' {
during Tcp.TcpConnection($id, Tcp.TcpListener(8001)) spawn named ['tcpConnection', id] {
assert Tcp.TcpAccepted(id);
on message Tcp.DataIn(id, $data) {
2018-11-18 17:01:11 +00:00
console.log('got', id, new Decoder(data).next());
send Tcp.DataOut(id, data);
2018-11-13 21:27:26 +00:00
}
}
}