Skip to content

Commit

Permalink
Add LidarViewer and User
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Nov 29, 2023
1 parent e2fff74 commit 4f1cf3e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 33 deletions.
50 changes: 43 additions & 7 deletions backend/src/controllers/WebSocketChat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
#include <drogon/PubSubService.h>
#include <drogon/HttpAppFramework.h>

#include <spdlog/spdlog.h>
#include <cpptrace/cpptrace.hpp>

#include <nlohmann/json.hpp>
//#include <nlohmann/json-schema.hpp>

using json = nlohmann::json;
//using json_validator = nlohmann::json_schema::json_validator;

class WebSocketChat : public drogon::WebSocketController<WebSocketChat>
{
public:
Expand All @@ -21,8 +30,15 @@ class WebSocketChat : public drogon::WebSocketController<WebSocketChat>
drogon::PubSubService<std::string> chatRooms_;
};

enum SubscriberType {
Unknown,
User,
Car,
};

struct Subscriber
{
SubscriberType type_;
std::string chatRoomName_;
drogon::SubscriberID id_;
};
Expand All @@ -31,16 +47,40 @@ void WebSocketChat::handleNewMessage(const drogon::WebSocketConnectionPtr& wsCon
std::string&& message,
const drogon::WebSocketMessageType& type)
{
LOG_DEBUG << "new websocket message:" << message;
if (type == drogon::WebSocketMessageType::Ping)
auto& subscriber = wsConnPtr->getContextRef<Subscriber>();
if (subscriber.type_ == SubscriberType::Unknown) {
if (type == drogon::WebSocketMessageType::Ping)
{
auto j = json::parse(message);
if (j["type"] == "user") {
subscriber.type_ = SubscriberType::User;
spdlog::debug("Received a ping from user: {} | WebSocketChat::handleNewMessage", wsConnPtr->peerAddr().toIp());
}
else if (j["type"] == "car") {
subscriber.type_ = SubscriberType::Car;
spdlog::debug("Received a ping from car: {} | WebSocketChat::handleNewMessage", wsConnPtr->peerAddr().toIp());
LOG_DEBUG << "";
}
}
return;
}
switch (type) {
case drogon::WebSocketMessageType::Text:
chatRooms_.publish(s.chatRoomName_, message);
break;
}
/*if (type == drogon::WebSocketMessageType::Ping)
{
s.type_ = SubscriberType::User;
LOG_DEBUG << "recv a ping";
LOG_DEBUG << "new websocket message:" << message;
}
else if (type == drogon::WebSocketMessageType::Text)
{
auto& s = wsConnPtr->getContextRef<Subscriber>();
chatRooms_.publish(s.chatRoomName_, message);
}
}*/
}

void WebSocketChat::handleConnectionClosed(const drogon::WebSocketConnectionPtr& conn)
Expand All @@ -53,10 +93,6 @@ void WebSocketChat::handleConnectionClosed(const drogon::WebSocketConnectionPtr&
void WebSocketChat::handleNewConnection(const drogon::HttpRequestPtr& req,
const drogon::WebSocketConnectionPtr& conn)
{
for (auto [k, v] : req->getCookies())
{
LOG_DEBUG << k << ":" << v;
}
LOG_DEBUG << "new websocket connection!";
conn->send("haha!!!");
Subscriber s;
Expand Down
11 changes: 11 additions & 0 deletions backend/src/room/User.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "User.h"

User::User()
{

}

User::~User()
{

}
16 changes: 16 additions & 0 deletions backend/src/room/User.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef USER_H
#define USER_H

#pragma once

class User
{
public:
User();
~User();

private:

};

#endif
14 changes: 7 additions & 7 deletions backend/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ set_languages("cxx17")

-- Logging / Data handling
add_requires("boost", { configs = {chrono = true} })
-- add_requires("fmt", "spdlog")
-- add_requires("nlohmann_json")
add_requires("fmt", "spdlog")
add_requires("nlohmann_json", "json-schema-validator")

-- C++ Backend API for Svelte App
add_requires("drogon")

-- For Functional Programming?
-- add_requires("tl_expected")
add_requires("tl_expected")

target("backend")
set_kind("binary")

add_packages("boost")
-- add_packages("fmt", "spdlog")
-- add_packages("nlohmann_json")
add_packages("fmt", "spdlog")
add_packages("nlohmann_json", "json-schema-validator")
add_packages("drogon")
-- add_packages("tl_expected")
add_packages("tl_expected")

before_build_files(function(target)
import("core.project.config")
Expand All @@ -47,7 +47,7 @@ target("backend")
os.execv("pnpm", {"--prefix", frontend_dir, "run", "build"})

print("Copying svelte files...")
os.cp(path.join(os.scriptdir(), "..", "frontend", "public"), static)
os.cp(path.join(os.scriptdir(), "..", "frontend", "build"), static)
print("Completed copying svelte files...")
end)

Expand Down
4 changes: 3 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.27.4",
"@types/node": "^20.10.0",
"@types/ws": "^8.5.10",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2"
"vite": "^4.4.2",
"ws": "^8.14.2"
},
"type": "module"
}
12 changes: 12 additions & 0 deletions frontend/src/lib/LidarViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
export let lidar_websocket_url: string;
let webSocket = new WebSocket(lidar_websocket_url);
webSocket.addEventListener("open", (event) => {
console.log("Open");
});
webSocket.addEventListener("message", (event) => {
console.log(event);
});
</script>

<canvas id="canvas" width="800" height="600"> </canvas>
17 changes: 17 additions & 0 deletions frontend/src/lib/LidarViewerDev.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
import { onMount } from "svelte";
import { WebSocket } from "ws";
onMount(() => {
let websocket_url = "ws://localhost:8848/lidar";
let webSocket = new WebSocket(websocket_url);
webSocket.addEventListener("open", (event) => {
console.log("Open");
});
webSocket.addEventListener("message", (event) => {
let message = JSON.parse(event.data.toString());
message["type"]
});
})
</script>

<canvas id="canvas" width="800" height="600"> </canvas>
55 changes: 37 additions & 18 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
<script lang="ts">
import { websocket_url } from './store.js';
websocket_url.subscribe((value) => {
console.log(value);
try {
let webSocket = new WebSocket(value);
webSocket.addEventListener("open", (event) => {
console.log("Open");
});
webSocket.addEventListener("message", (event) => {
console.log(event);
});
} catch (e) {
console.log(e);
import { dev } from "$app/environment";
import { onMount } from "svelte";
import { websocket_url } from "./store.js";
import LidarViewerDev from "$lib/LidarViewerDev.svelte";
import LidarViewer from "$lib/LidarViewer.svelte";
// websocket_url.subscribe((value) => {
// console.log(value);
// try {
// let webSocket = new WebSocket(value);
// webSocket.addEventListener("open", (event) => {
// console.log("Open");
// });
// webSocket.addEventListener("message", (event) => {
// console.log(event);
// });
// } catch (e) {
// console.log(e);
// }
// });
// function set_websocket_url(event: Event & { target: HTMLInputElement }) {
// websocket_url.set(event.target.value);
// }
onMount(() => {
if (dev) {
websocket_url.set("ws://localhost:8848/chat");
} else {
websocket_url.set("ws://" + location.hostname + "/chat");
}
});
function set_websocket_url(event: Event & { target: HTMLInputElement }) {
websocket_url.set(event.target.value);
}
</script>

<h1>Welcome to SvelteKit</h1>
<p>
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
</p>
<input on:input={set_websocket_url} />

{#if dev}
<LidarViewerDev lidar_websocket_url={"ws://localhost:8848/chat"}></LidarViewerDev>
{:else}
<!-- <LidarViewer lidar_websocket_url={$websocket_url}>
</LidarViewer> -->
{/if}

0 comments on commit 4f1cf3e

Please sign in to comment.