-
-
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.
Merge pull request #96 from EliasDeHondt/websockets
Websockets
- Loading branch information
Showing
8 changed files
with
178 additions
and
63 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var upgrader = websocket.Upgrader{ | ||
CheckOrigin: func(r *http.Request) bool { | ||
origin := r.Header.Get("Origin") | ||
return origin == GetFrontendIP() | ||
}, | ||
} | ||
|
||
func HandleMetricsSocket(ctx *gin.Context) { | ||
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil) | ||
if err != nil { | ||
log.Println("Error upgrading metrics socket:", err) | ||
return | ||
} | ||
defer func(conn *websocket.Conn) { | ||
err := conn.Close() | ||
if err != nil { | ||
log.Println("Error closing metrics socket:", err) | ||
} | ||
}(conn) | ||
|
||
for { | ||
stats, err := c.GetTotalUsage() | ||
if err != nil { | ||
log.Println("Error getting metrics stats:", err) | ||
return | ||
} | ||
|
||
err = conn.WriteJSON(stats) | ||
if err != nil { | ||
log.Println("Error writing metrics stats:", err) | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | ||
log.Println("WebSocket connection closed by client.") | ||
} | ||
break | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
} |
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
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,54 @@ | ||
import {Injectable, OnDestroy} from "@angular/core"; | ||
import {Subject} from "rxjs"; | ||
import {Metrics} from "../domain/Metrics"; | ||
import {environment} from "../../environments/environment"; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class StatWebSocketService implements OnDestroy { | ||
private url = `${environment.BASE_URL}/secured/statsocket`; | ||
private socket!: WebSocket; | ||
private messagesSubject: Subject<Metrics> = new Subject() | ||
|
||
constructor() { | ||
} | ||
|
||
connect(): void { | ||
this.socket = new WebSocket(this.url); | ||
|
||
this.socket.onopen = () => { | ||
} | ||
|
||
this.socket.onmessage = (event: MessageEvent) => { | ||
try { | ||
const data: Metrics = JSON.parse(event.data); | ||
this.messagesSubject.next(data) | ||
} catch (error) { | ||
console.error("[WebSocketService] Error parsing data:", error) | ||
} | ||
} | ||
|
||
this.socket.onerror = () => { | ||
console.error("[WebSocketService] WebSocket Error: " + this.url); | ||
} | ||
|
||
this.socket.onclose = () => { | ||
} | ||
} | ||
|
||
disconnect(): void { | ||
if (this.socket && this.socket.readyState === WebSocket.OPEN) { | ||
this.socket.close(); | ||
} | ||
} | ||
|
||
getMetrics() { | ||
return this.messagesSubject.asObservable(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.disconnect(); | ||
} | ||
|
||
} |
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