Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement websocket server-client communication #22

Draft
wants to merge 57 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e300ba5
group binding modules by folder name
NovaSagittarii Dec 28, 2023
6dffec9
setup vite for client
NovaSagittarii Dec 29, 2023
cc972ad
bind vector2 zero-parameter constructor
NovaSagittarii Dec 30, 2023
33980c9
improve cell-level accessor/mutators for client usage
NovaSagittarii Dec 30, 2023
f878448
modularize wasm so vite can load the default module
NovaSagittarii Dec 30, 2023
1928e84
setup vite configs
NovaSagittarii Dec 30, 2023
d8d8130
add basic tests for emscripten compiled library
NovaSagittarii Dec 30, 2023
cf649de
add basic typing file for conway library
NovaSagittarii Dec 30, 2023
c9d974d
set up a basic interactive life grid
NovaSagittarii Dec 30, 2023
33d0d4f
implement socket connection logger in tsx
NovaSagittarii Dec 30, 2023
ca48e22
move structure and structure_properties protos into //conway
NovaSagittarii Dec 30, 2023
1ddda23
update vector2 visibility to be accessible from //conway and //conway…
NovaSagittarii Dec 30, 2023
768f20e
setup build rules for //conway protos
NovaSagittarii Dec 30, 2023
9ef8beb
move structure_lookup.proto into //conway_rts
NovaSagittarii Dec 30, 2023
f2cc7f7
setup build rules for //conway_rts protos
NovaSagittarii Dec 30, 2023
1e535b9
delete /external/*/CMakeLists.txt
NovaSagittarii Dec 30, 2023
84303a9
move //external to //externals
NovaSagittarii Dec 30, 2023
4660a52
setup autocomplete from bazel generated files
NovaSagittarii Dec 30, 2023
1dc8861
setup basic server-client communication (only register name and query…
NovaSagittarii Dec 31, 2023
fa95923
display blob bytes as hex
NovaSagittarii Dec 31, 2023
47add26
implement adding clients into room
NovaSagittarii Jan 1, 2024
3ab8937
implement Vector2::CopyToProtobuf() method
NovaSagittarii Jan 2, 2024
268602f
bump to bazel 7.0.0
NovaSagittarii Jan 2, 2024
613e21f
migrate zlib into MODULE.bazel
NovaSagittarii Jan 2, 2024
10e0189
implement ::CopyToProtobuf() methods for //conway/... targets
NovaSagittarii Jan 2, 2024
ef94497
update //conway_rts:client dependencies
NovaSagittarii Jan 2, 2024
ad8275b
implement partial ::CopyToProtobuf() method support for //conway_rts/…
NovaSagittarii Jan 2, 2024
2b37dd2
implement clients joining a room from room list
NovaSagittarii Jan 2, 2024
7808216
bump emsdk version to fix windows FileExistsError
NovaSagittarii Jan 3, 2024
060201e
fix -Wreorder-ctr (enabled in emcc)
NovaSagittarii Jan 3, 2024
c3ef9f4
add //conway_rts:client and //conway_rts:team emscripten bindings
NovaSagittarii Jan 3, 2024
9ea84bc
add //conway_rts:room emscripten bindings
NovaSagittarii Jan 3, 2024
de02cb6
cleanup //client_wasm BUILD, format
NovaSagittarii Jan 3, 2024
9947ca6
handle client-server RoomCreate and RoomJoin events
NovaSagittarii Jan 3, 2024
71dac41
change A: () => Z format to A(): Z
NovaSagittarii Jan 3, 2024
4320fc8
add delete() method to ts interfaces
NovaSagittarii Jan 3, 2024
fc81e27
fill in missing emscripten bindings
NovaSagittarii Jan 4, 2024
2acf052
switch @usocket git_repository to http_archive
NovaSagittarii Jan 4, 2024
de52266
handle negative offset out of bounds
NovaSagittarii Jan 5, 2024
42ef6cb
setup basic client that uses life_grid wasm and allows loading
NovaSagittarii Jan 5, 2024
ddde14e
don't track MODULE.bazel.lock for now
NovaSagittarii Jan 5, 2024
99ba846
update dependencies installation instructions for windows
NovaSagittarii Jan 5, 2024
6b2500c
remove unneeded c++17 flag in linux for boringssl
NovaSagittarii Jan 6, 2024
a15ac55
linux compatibility for @libuv
NovaSagittarii Jan 6, 2024
f981f80
reduce COPTS c++ version for @lsquic
NovaSagittarii Jan 6, 2024
95db335
add files neccessary for linux to build @lsquic
NovaSagittarii Jan 6, 2024
d7621c6
fix linux COPTS
NovaSagittarii Jan 6, 2024
083dd83
Merge branches 'NovaSagittarii/websocket' and 'NovaSagittarii/websock…
NovaSagittarii Jan 6, 2024
4622a72
document linux dependencies
NovaSagittarii Jan 6, 2024
8d0f57f
add colored corners for rotational orientation
NovaSagittarii Jan 6, 2024
5819be4
swap colors to lessen constrast
NovaSagittarii Jan 6, 2024
db53170
bounds checking for LifeGrid::Compare
NovaSagittarii Jan 6, 2024
3b10545
add preview data and structure switching
NovaSagittarii Jan 6, 2024
cbb8a47
use spacebar to place instead of onclick (since drag is used to look …
NovaSagittarii Jan 6, 2024
e7c5441
increase grid size
NovaSagittarii Jan 6, 2024
29fab37
enable compiler optimizations for emscripten
NovaSagittarii Jan 6, 2024
0bb5636
update emscripten deps version
NovaSagittarii Apr 21, 2024
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
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.3.2
7.0.0
21 changes: 21 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
root: true,
env: {
browser: true,
es2020: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
ignorePatterns: ["build", "bazel-*", "dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ CMakeCache.txt
bazel-bin
bazel-conway-rts
bazel-out
bazel-testlogs
bazel-testlogs

# removeMODULE.bazel.lock after modules have been migrated
MODULE.bazel.lock
9 changes: 9 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bazel-*
build
dist
node_modules

**/*.html
**/*.md

pnpm-lock.yaml
7 changes: 7 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# load("//tools/protobufjs:rules.bzl", "protobufjs_library")

load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

refresh_compile_commands(
name = "refresh_compile_commands",
# targets = ["//server"],
)

filegroup(
name = "conway",
srcs = [
Expand Down
10 changes: 10 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ There's emsdk in WORKSPACE but it's not added to the Bazel Central Registry yet
"""

bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "zlib", version = "1.3")

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
commit = "9d438afd4febf622aa05b9a267c09439717e58b9",
)
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# conway-rts
conway rts cyber project (but still being designed)

## Dependencies

### Windows
- [chocolately](https://chocolatey.org/install)
- [bazelisk](https://github.com/bazelbuild/bazelisk)
- [git](https://community.chocolatey.org/packages/git.install)
- [python](https://community.chocolatey.org/packages/python/3.12.1)
- [nvm](https://community.chocolatey.org/packages/nvm)
- [VS Build Tools, C++ compiler](https://visualstudio.microsoft.com/downloads/?q=build+tools#build-tools-for-visual-studio-2022)

### Linux
- [bazelisk](https://github.com/bazelbuild/bazelisk)
- gcc version 8+ (using c++2a), ideally gcc version 10+ (for c++20 support)
- [gsrc](https://www.gnu.org/software/gsrc/) can be used if pre-built binaries aren't available.

## Running
Install [bazelisk](https://github.com/bazelbuild/bazelisk)
```
bazelisk # installs bazel with specific version
bazel query ... # list targets
bazel test ... --config=cl # run all tests (windows, cl.exe)
bazel test ... --config=gcc # run all tests (linux, gcc)
balze build ... # build all targets (config pattern still applies)
bazel query ... # list targets
bazel test ... # run all tests
bazel build ... # build all targets (config pattern still applies)
bazel run //:refresh_compile_commands # initialize intellisense files
bazel run //server # builds and starts the game server
npm run test # runs vitest (clientside tests)
npm run dev # runs the client
```

# Rough UML Diagram
Expand Down
35 changes: 14 additions & 21 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
########################
# Emscripten toolchain #
########################
http_archive(
git_repository(
name = "emsdk",
sha256 = "5dd94e557b720800a60387ec078bf3b3a527cbd916ad74a696fe399f1544474f",
strip_prefix = "emsdk-3.1.46/bazel",
url = "https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.46.tar.gz",
commit = "9347bc393b94a17b93450bbc98bc3f66cef2aeb0", # 3.1.51 + 2 commits (to patch windows bazel wasm_cc_binary)
remote = "git@github.com:emscripten-core/emsdk.git",
strip_prefix = "bazel",
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")
Expand All @@ -56,7 +56,7 @@ emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")

emsdk_emscripten_deps(emscripten_version = "3.1.46")
emsdk_emscripten_deps(emscripten_version = "3.1.51")

load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")

Expand All @@ -67,46 +67,39 @@ register_emscripten_toolchains()
###############
http_archive(
name = "uwebsockets",
build_file = "BUILD.uwebsockets.bazel",
build_file = "@//externals:BUILD.uwebsockets.bazel",
sha256 = "6794e7895eb8cc182024a0ae482a581eaa82f55f7cca53ae88b30738449f3cb9",
strip_prefix = "uWebSockets-20.51.0",
url = "https://github.com/uNetworking/uWebSockets/archive/refs/tags/v20.51.0.tar.gz",
)

git_repository(
http_archive(
name = "usockets",
build_file = "BUILD.usockets.bazel",
commit = "8cd4cb66eb061b2594ca114b9ea1ead64613ad4b", # v0.8.6
remote = "git@github.com:uNetworking/uSockets.git",
build_file = "@//externals:BUILD.usockets.bazel",
integrity = "sha256-FuuhM90z6t4vX43YdhLAS13XEQZuBHHGDWQaL2qYjxY=",
strip_prefix = "uSockets-0.8.6",
url = "https://github.com/uNetworking/uSockets/archive/refs/tags/v0.8.6.tar.gz",
)

git_repository(
name = "boringssl",
build_file = "BUILD.boringssl.bazel",
build_file = "@//externals:BUILD.boringssl.bazel",
commit = "1ccef4908ce04adc6d246262846f3cd8a111fa44", # specific version used with usockets v0.8.6
remote = "git@github.com:google/boringssl.git",
)

git_repository(
name = "lsquic",
build_file = "BUILD.lsquic.bazel",
build_file = "@//externals:BUILD.lsquic.bazel",
commit = "3bbf683f25ab84826951350c57ae226c88c54422", # v3.2.0
recursive_init_submodules = True,
remote = "git@github.com:litespeedtech/lsquic.git",
)

http_archive(
name = "libuv",
build_file = "BUILD.libuv.bazel",
build_file = "@//externals:BUILD.libuv.bazel",
sha256 = "d50af7e6d72526db137e66fad812421c8a1cae09d146b0ec2bb9a22c5f23ba93",
strip_prefix = "libuv-1.47.0",
url = "https://github.com/libuv/libuv/archive/refs/tags/v1.47.0.tar.gz",
)

http_archive(
name = "zlib",
build_file = "BUILD.zlib.bazel",
sha256 = "ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e",
strip_prefix = "zlib-1.3",
url = "https://github.com/madler/zlib/releases/download/v1.3/zlib-1.3.tar.gz",
)
2 changes: 1 addition & 1 deletion cc_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ CC Compiler options to be used for all project files.

CC_OPTS = select({
"@bazel_tools//src/conditions:windows": ["/std:c++20"],
"//conditions:default": ["--std=c++20"],
"//conditions:default": ["-std=c++20"],
})
13 changes: 13 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initia-scale=1.0" />
<title>client test</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
54 changes: 27 additions & 27 deletions client/public/room-interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,76 @@ const server_url = "ws://localhost:3000";

// js for fast debugging? (at least won't need to set up jsx tooling)
function CreateButton(label, callback) {
const el = document.createElement('button');
const el = document.createElement("button");
el.innerText = label;
el.addEventListener('click', callback);
el.addEventListener("click", callback);
return el;
}

class ClientInstance {
constructor() {
this.ws = null;
const body = document.createElement('div');
const body = document.createElement("div");
const info = {
'logs': document.createElement('div'),
'room': document.createElement('div'),
logs: document.createElement("div"),
room: document.createElement("div"),
};
this.element = {
'info': document.createElement('div'),
'connect': CreateButton("Connect", () => this.Connect()),
'disconnect': CreateButton("Disconnect", () => this.Disconnect()),
'payload': document.createElement('input'),
'send': CreateButton("Send", () => {
info: document.createElement("div"),
connect: CreateButton("Connect", () => this.Connect()),
disconnect: CreateButton("Disconnect", () => this.Disconnect()),
payload: document.createElement("input"),
send: CreateButton("Send", () => {
this.Send(this.element.payload.value);
this.element.payload.value = "";
}),
};
this.element.info.append(...Object.values(info));
this.element.info.classList.add('info');
this.element.info.classList.add("info");
body.append(...Object.values(this.element));
this.element = Object.assign(this.element, info);
this.element.body = body;
console.log(this.element)
console.log(this.element);
this.logs = [];
for(let i = 0; i < 10; i ++) this.PushLog(-1, '');
for (let i = 0; i < 10; i++) this.PushLog(-1, "");
}
Connect() {
if(this.ws) this.Disconnect();
if (this.ws) this.Disconnect();
this.ws = new WebSocket(server_url);
this.ws.onopen = (event) => {
this.PushLog(event.timeStamp, 'socket opened');
}
this.PushLog(event.timeStamp, "socket opened");
};
this.ws.onclose = (event) => {
this.PushLog(event.timeStamp, 'socket closed');
}
this.PushLog(event.timeStamp, "socket closed");
};
this.ws.onerror = (event) => {
console.warn(event);
this.PushLog(event.timeStamp, 'socket error');
}
this.PushLog(event.timeStamp, "socket error");
};
this.ws.onmessage = (event) => {
console.log('data', event.data);
console.log("data", event.data);
this.PushLog(event.timeStamp, `<- ${event.data}`);
}
};
}
Disconnect() {
this.ws.close();
this.ws = null;
}
PushLog(time, log) {
this.logs.push(`[${(~~time).toString().padStart(6, '_')}] ${log}`);
this.element.logs.innerText = this.logs.slice(-10).join('\n');
this.logs.push(`[${(~~time).toString().padStart(6, "_")}] ${log}`);
this.element.logs.innerText = this.logs.slice(-10).join("\n");
}
Send(data) {
if(!this.ws) throw 'socket is not open; cannot send';
if (!this.ws) throw "socket is not open; cannot send";
this.ws.send(data);
this.PushLog(new Event('').timeStamp, `-> ${data}`)
this.PushLog(new Event("").timeStamp, `-> ${data}`);
}
}

const c = new ClientInstance();
document.body.append(c.element.body);

const c2 = [...new Array(2)].map(x => {
const c2 = [...new Array(2)].map((x) => {
const client = new ClientInstance();
document.body.append(client.element.body);
return client;
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect, useState } from "react";
import SocketLogger from "./SocketLogger";
import SocketUtils from "./SocketUtils";
import { ClientWebSocket } from "./Socket";
import RoomListing from "./RoomListing";
import Lobby from "./Lobby";
import GameClient from "./GameClient";

const server_url = "ws://localhost:3000";

function App() {
const [clientSocket, setClientSocket] = useState<ClientWebSocket | null>(
null,
);

useEffect(() => {
const s = new ClientWebSocket(server_url);
setClientSocket(s);
return () => s?.close();
}, []);

return (
<>
<div>Hello world</div>
{clientSocket && <SocketUtils socket={clientSocket.socket} />}
{clientSocket && <SocketLogger socket={clientSocket.socket} />}
{clientSocket && <RoomListing clientSocket={clientSocket} />}
{clientSocket && <Lobby clientSocket={clientSocket} />}
{clientSocket && <GameClient clientSocket={clientSocket} />}
</>
);
}

export default App;
Loading