-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working session-based mqtt connection BE & FE
- Loading branch information
linomp
committed
Mar 28, 2024
1 parent
d5a6a4c
commit bee040b
Showing
12 changed files
with
168 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
VITE_DEBUG= | ||
VITE_API_BASE=http://localhost:8000 | ||
VITE_MQTT_HOST= | ||
VITE_MQTT_TOPIC_PREFIX= | ||
VITE_MQTT_USERNAME= | ||
VITE_MQTT_PASSWORD= | ||
VITE_DEBUG=1 | ||
VITE_API_BASE=http://localhost:8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
build | ||
node_modules | ||
.env.development.secret | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* generated using openapi-typescript-codegen -- do no edit */ | ||
/* istanbul ignore file */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
|
||
export type GameMetrics = { | ||
active_sessions: number; | ||
total_started_games: number; | ||
total_ended_games: number; | ||
total_abandoned_games: number; | ||
total_seconds_played: number; | ||
avg_game_duration: number; | ||
min_game_duration?: number; | ||
max_game_duration?: number; | ||
}; |
12 changes: 12 additions & 0 deletions
12
mvp/client/ui/src/api/generated/models/MqttFrontendConnectionDetails.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* generated using openapi-typescript-codegen -- do no edit */ | ||
/* istanbul ignore file */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
|
||
export type MqttFrontendConnectionDetails = { | ||
host: string; | ||
port: number; | ||
username: string; | ||
password: string; | ||
base_topic: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import mqtt from "mqtt"; | ||
import type { MqttFrontendConnectionDetails } from "src/api/generated"; | ||
|
||
export const getClient = async (connectionDetails: MqttFrontendConnectionDetails): Promise<mqtt.MqttClient> => { | ||
|
||
const brokerUrl = `wss://${connectionDetails.host}:${connectionDetails.port}/mqtt`; | ||
|
||
try { | ||
|
||
if (import.meta.env.VITE_DEBUG) { | ||
console.log(`Connecting to MQTT broker at ${brokerUrl}`); | ||
console.log(`Base topic: `, connectionDetails.base_topic); | ||
} | ||
|
||
const client = await mqtt.connectAsync( | ||
brokerUrl, | ||
{ | ||
username: connectionDetails.username, | ||
password: connectionDetails.password, | ||
} | ||
); | ||
|
||
client.subscribe(`${connectionDetails.base_topic}/#`, | ||
(err) => { | ||
if (err) { | ||
console.error("Error subscribing to topic: ", err); | ||
} | ||
} | ||
); | ||
|
||
if (import.meta.env.VITE_DEBUG) { | ||
client.on("message", (topic, message) => { | ||
console.log(`Received message on topic ${topic}: ${message.toString()}`); | ||
}); | ||
} | ||
|
||
return client; | ||
|
||
} catch (error) { | ||
console.error(`Error connecting to MQTT broker ${brokerUrl}: `, error); | ||
throw error; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from pydantic import BaseModel | ||
|
||
load_dotenv() | ||
|
||
MQTT_HOST = os.environ.get("MQTT_HOST", "test.mosquitto.org") | ||
MQTT_WSS_PORT = int(os.environ.get("MQTT_WSS_PORT", 1883)) | ||
MQTT_FE_USER = os.environ.get("MQTT_FE_USER") | ||
MQTT_FE_PASSWORD = os.environ.get("MQTT_FE_PASSWORD") | ||
MQTT_TOPIC_PREFIX = os.environ.get("MQTT_TOPIC_PREFIX", "pdmgame/clients") | ||
|
||
|
||
class MqttFrontendConnectionDetails(BaseModel): | ||
host: str | ||
port: int | ||
username: str | ||
password: str | ||
base_topic: str | ||
|
||
def __init__(self, session_id: str): | ||
super().__init__( | ||
host=MQTT_HOST, | ||
port=MQTT_WSS_PORT, | ||
username=MQTT_FE_USER, | ||
password=MQTT_FE_PASSWORD, | ||
base_topic=f"{MQTT_TOPIC_PREFIX}/{session_id}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters