Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
3 changes: 0 additions & 3 deletions README.md

This file was deleted.

37 changes: 37 additions & 0 deletions backend/broadcaster.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
bring cloud;
bring websockets;
bring ui;

pub class Broadcaster {
pub url: str;
server: websockets.WebSocket;
clients: cloud.Bucket;

new() {
this.server = new websockets.WebSocket(name: "counter_updates") as "counter_updates";
this.url = this.server.url;
this.clients = new cloud.Bucket();

// upon connection, add the client to the list
this.server.onConnect(inflight(id: str): void => {
this.clients.put(id, "");
});

// upon disconnect, remove the client from the list
this.server.onDisconnect(inflight(id: str): void => {
this.clients.delete(id);
});

// Custom resource display in Console UI
// hide the websockets server and cloud bucket resources from the Console UI
nodeof(this.server).hidden = true;
nodeof(this.clients).hidden = true;
}

// send a message to all clients
pub inflight broadcast(messgae: str) {
for id in this.clients.list() {
this.server.sendMessage(id, messgae);
}
}
}
48 changes: 48 additions & 0 deletions backend/main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
bring cloud;
bring expect;
bring vite;
// bring "./vite/vite.w" as vite;
bring http;
bring "./broadcaster.w" as broadcaster;

// Winglang doesn't have a built-in support for __dirname yet, so we use a workaround to get the current directory.
// @see tracking issue: https://github.com/winglang/wing/issues/3736
class Utils {
extern "utils.js" pub static __dirname(): str;
}

let myBroadcaster = new broadcaster.Broadcaster() as "Broadcaster";
let api = new cloud.Api(cors: true);
let counter = new cloud.Counter();

let website = new vite.Vite(
root: "{Utils.__dirname()}/../frontend",
publicEnv: {
TITLE: "Wing + Vite + React",
API_URL: api.url,
WS_URL: myBroadcaster.url
},
) as "Vite Website";

api.get("/counter", inflight () => {
return {
body: "{counter.peek()}"
};
});

api.post("/counter", inflight () => {
let prev = counter.inc();
myBroadcaster.broadcast("refresh");
return {
body: "{prev + 1}"
};
});

// test "api counter increment and get" {
// log("counter initial value: {counter.peek()}");
// assert(counter.peek() == 0);
// http.post(api.url + "/counter");
// let res = http.get(api.url + "/counter");
// log("counter value after increment: {res.body}");
// assert(res.body == "1");
// }
Loading