# megatran/hololens_websocket_uwp architecture

> Architecture diagram of the GitHub repository megatran/hololens_websocket_uwp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/megatran/hololens_websocket_uwp
- Repository: https://github.com/megatran/hololens_websocket_uwp

## Overview

##### Project Type & Purpose
- This is a Unity-based Mixed Reality client (UWP/HoloLens) that communicates over WebSockets to a multi-platform WebSocket server (Python or NodeJS).
- The Assets folder is the Unity project (with HoloToolkit/MRTK packages and custom C# scripts), and the python-websocket folder contains simple echo/chat server examples.

##### Architectural Components
- Unity/MRTK Client (UWP App on HoloLens)
- C# scripts under Assets/Scripts and Assets/HoloToolkit handle WebSocket connections.
- TestWebsocketSender.cs uses the UwpWebsocket wrapper to open “ws://…:8000/” and send/receive text messages.
- Scene “holoSocket.unity” hosts an input mechanism or display logic to show server messages.
- WebSocket Server
- Python echo_socket_server.py (simple echo behavior)
- Python chat_server.py (bidirectional chat)
- Or a NodeJS ws-based server.
- Optional Web Client
- websocket.html in python-websocket for testing in a browser.
- Network Infrastructure
- HoloLens device and server must share a network (Wi-Fi or Ethernet), reachable via the specified URI.

##### Interactions & Data Flows
- The HoloLens app (WebSocket Client) establishes a persistent TCP/WebSocket connection to the server endpoint on port 8000.
- User input or gaze gestures in Unity become messages sent over that socket.
- The server broadcasts incoming messages to all connected clients (HoloLens, browser, CLI).
- Responses or echoes flow back over the same connection and are rendered by the Unity app’s UI.

##### Key Architectural Patterns
- Publish/Subscribe over WebSocket: the server acts as a message broker, broadcasting to all subscribers.
- Component separation in Unity:
- Presentation/Scene Assets (Scenes, Prefabs)
- Communication Layer (TestWebsocketSender.cs, UwpWebsocket.cs)
- Toolkit/Utilities (HoloToolkit modules provide input, boundary, spatial mapping, etc.)

##### Technologies & Frameworks
- Unity 2018+ targeting UWP/HoloLens
- Microsoft Mixed Reality Toolkit (HoloToolkit) for input, UX, spatial mapping
- C# UwpWebsocket library (Assets/Scripts/UwpWebsocket.cs)
- Python 3 + simple-websocket-server or NodeJS ws module
- Optional web front-end (HTML/JS) for browser connectivity

## Diagram (Mermaid)

```mermaid
graph TD
    %% Deployment Context
    subgraph "HoloLens Device (UWP App)"
        style UnityApp fill:#9f6,stroke:#080
        UnityApp["Unity/MRTK Client"]:::client
        click UnityApp "https://github.com/megatran/hololens_websocket_uwp/tree/master/Assets"

        subgraph "Mixed Reality Toolkit"
            style ToolkitModules fill:#cfc,stroke:#080
            ToolkitModules["HoloToolkit Modules"]:::toolkit
            click ToolkitModules "https://github.com/megatran/hololens_websocket_uwp/tree/master/Assets/HoloToolkit"
        end

        subgraph "MR UI Layer"
            style MRUIScene fill:#9f6,stroke:#080
            MRUIScene["Scene: holoSocket.unity"]:::client
            click MRUIScene "https://github.com/megatran/hololens_websocket_uwp/blob/master/Assets/Scenes/holoSocket.unity"
        end

        subgraph "Communication Layer"
            style WebSocketLib fill:#9f6,stroke:#080
            TestWebsocketSender["TestWebsocketSender.cs"]:::client
            click TestWebsocketSender "https://github.com/megatran/hololens_websocket_uwp/blob/master/Assets/Scripts/TestWebsocketSender.cs"
            UwpWebsocket["UwpWebsocket.cs"]:::client
            click UwpWebsocket "https://github.com/megatran/hololens_websocket_uwp/blob/master/Assets/Scripts/UwpWebsocket.cs"
        end
    end

    subgraph "Server Host"
        style WebSocketServer fill:#6cf,stroke:#039
        WebSocketServer["WebSocket Server"]:::server

        EchoServer["echo_socket_server.py"]:::server
        click EchoServer "https://github.com/megatran/hololens_websocket_uwp/blob/master/python-websocket/echo_socket_server.py"
        ChatServer["chat_server.py"]:::server
        click ChatServer "https://github.com/megatran/hololens_websocket_uwp/blob/master/python-websocket/chat_server.py"
        SimpleServerDir["simple-websocket-server"]:::server
        click SimpleServerDir "https://github.com/megatran/hololens_websocket_uwp/tree/master/python-websocket/simple-websocket-server"
    end

    subgraph "Optional/Test Clients" 
        direction TB
        style BrowserTest fill:#eee,stroke:#999,stroke-dasharray: 5 5
        BrowserTest["Browser Test Page"]:::optional
        click BrowserTest "https://github.com/megatran/hololens_websocket_uwp/blob/master/python-websocket/websocket.html"
        CLIClient["CLI Client"]:::optional
    end

    %% Data Flows
    ToolkitModules -->|User Input/Gaze| TestWebsocketSender
    MRUIScene -->|Display Messages| ToolkitModules
    TestWebsocketSender -->|ws://…:8000 Connect/Send Message| WebSocketServer
    UwpWebsocket -->|Handles WS Protocol| TestWebsocketSender

    WebSocketServer -->|Broadcast/Echo Messages| TestWebsocketSender
    WebSocketServer -->|Broadcast/Echo Messages| BrowserTest
    WebSocketServer -->|Broadcast/Echo Messages| CLIClient

    %% Legend Styles
    classDef client fill:#9f6,stroke:#080
    classDef server fill:#6cf,stroke:#039
    classDef optional fill:#eee,stroke:#999,stroke-dasharray: 5 5
    classDef toolkit fill:#cfc,stroke:#080
```
