From b3ea7a0ffcc24a3363d6fb507812438fe0d1ff16 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Sun, 19 Jul 2026 20:13:23 +0500 Subject: init --- back/Socket/Pool.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ back/Socket/Socket.go | 57 ++++++++++++++++++++++++++++++++++++++++++ back/Socket/WasmProvider.go | 20 +++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 back/Socket/Pool.go create mode 100644 back/Socket/Socket.go create mode 100644 back/Socket/WasmProvider.go (limited to 'back/Socket') diff --git a/back/Socket/Pool.go b/back/Socket/Pool.go new file mode 100644 index 0000000..f3d4687 --- /dev/null +++ b/back/Socket/Pool.go @@ -0,0 +1,60 @@ +package socket + +import ( + "encoding/base64" + "crypto/rand" + + "golang.org/x/net/websocket" +) + +type Connection struct { + UUID string + WS *websocket.Conn +} + +var connPool map[string]*Connection + +func generateSecureString(length int) (string, error) { + // Calculate required byte length to yield the requested string length after Base64 encoding + byteLength := (length * 3) / 4 + if (length * 3) % 4 != 0 { + byteLength++ + } + + bytes := make([]byte, byteLength) + if _, err := rand.Read(bytes); err != nil { + return "", err + } + + // Encode to string and trim to exact requested length + encoded := base64.URLEncoding.EncodeToString(bytes) + return encoded[:length], nil +} + +func Init() { + connPool = make(map[string]*Connection) +} + + +func (self *Connection) Register() error { + var nameExists = true + var new_id string + for nameExists { + new_id, err := generateSecureString(8) + if err != nil { + return err + } + _, nameExists = connPool[new_id] + } + self.UUID = new_id + connPool[new_id] = self + return nil +} + +func RemoveFromPool(name string) { + var _, exists = connPool[name] + if (!exists) { + return + } + delete(connPool, name) +} diff --git a/back/Socket/Socket.go b/back/Socket/Socket.go new file mode 100644 index 0000000..554674b --- /dev/null +++ b/back/Socket/Socket.go @@ -0,0 +1,57 @@ +package socket + +import ( + "fmt" + "net/http" + + "golang.org/x/net/websocket" +) + +func RegisterEndpoints() { + http.Handle("/ws", websocket.Handler(handleWS)) +} + +func handleWS(conn *websocket.Conn) { + fmt.Printf("%v connecting\n", conn.Config().Origin); + defer conn.Close() + + // Adding to the pool + var connCache = Connection { + UUID: "", + WS: conn, + } + + var regErr = connCache.Register() + if regErr != nil { + fmt.Printf("Failed to register the connection: %s\n", regErr.Error()); + return + } + + if err := websocket.Message.Send(conn, GetModule()); err != nil { + fmt.Printf("Failed to send initial module: %s\n", err.Error()) + return + } + + var buf = make([]byte, 1024) + var length, err = conn.Read(buf) + if err != nil { + fmt.Printf("Connection closed or error: %v\n", err) + return + } + fmt.Printf("Recieved %v from WS\n", string(buf[:length])) + + if err := websocket.Message.Send(conn, []byte("xdx")); err != nil { + fmt.Printf("Failed to send initial module: %s\n", err.Error()) + return + } + + for { + length, err = conn.Read(buf) + if err != nil { + fmt.Printf("Connection closed or error: %v\n", err) + break + } + fmt.Printf("Recieved \"%v\" from WS\n", string(buf[:length])) + // conn.Write(buf[:length]) + } +} diff --git a/back/Socket/WasmProvider.go b/back/Socket/WasmProvider.go new file mode 100644 index 0000000..0041f22 --- /dev/null +++ b/back/Socket/WasmProvider.go @@ -0,0 +1,20 @@ +package socket + +import "os" + +const MOD_LOCATION = "../mod.wasm" + +var mod []byte + +func ReadMod() error { + var data, err = os.ReadFile(MOD_LOCATION) + if err != nil { + return err + } + mod = data + return nil +} + +func GetModule() []byte { + return mod +} -- cgit v1.3