Skip to content

Commit

Permalink
improve websocket connection and response with connected users
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjoz committed May 27, 2024
1 parent 276abaf commit 6dd70c9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
26 changes: 22 additions & 4 deletions backend/core/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/DmitriyVTitov/size"
"github.com/andybalholm/brotli"
"github.com/aws/aws-lambda-go/events"
"github.com/gorilla/websocket"
)

type HandlerArgs struct {
Expand All @@ -30,16 +31,18 @@ type HandlerArgs struct {
QueryString string
Method string
Route string
ClientID string // websocket
ConnectionID string // websocket
Authorization string
MergedID int32
ResponseBody *string
ResponseError string
ReqParams string
Usuario IUsuario
EventType string
IsWebSocket bool
// websocket
IsWebSocket bool
ClientID string
ConnectionID string
WebSocketConn *websocket.Conn
}

func PrintMemUsage() {
Expand Down Expand Up @@ -580,6 +583,22 @@ func makeHeaders() map[string]string {

// Crea una respuesta serializando un struct
func (req *HandlerArgs) MakeResponse(respStruct any) HandlerResponse {
if req.IsWebSocket {
bodyBytes, err := json.Marshal(respStruct)
if err != nil {
return req.MakeErr("No se pudo serializar respuesta: " + err.Error())
}
var bodyCompressed bytes.Buffer
gz := gzip.NewWriter(&bodyCompressed)
defer gz.Close()
if _, err := gz.Write(bodyBytes); err != nil {
log.Fatal(err)
}
if req.WebSocketConn != nil {
req.WebSocketConn.WriteMessage(websocket.BinaryMessage, bodyCompressed.Bytes())
}
return HandlerResponse{}
}
return MakeResponse(req, &respStruct)
}

Expand Down Expand Up @@ -608,7 +627,6 @@ func MakeResponse[T any](req *HandlerArgs, respStruct *T) HandlerResponse {
fileName := fmt.Sprintf("output-%v", req.MergedID)
response.BodyOnDisk = EncodeJsonToFileX(respStruct, fileName)
}

return response
}

Expand Down
21 changes: 19 additions & 2 deletions backend/handlers/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,35 @@ func MakeClientTable() aws.DynamoTableRecords[RtcClientOffer] {
}

func PostRtcOffer(args *core.HandlerArgs) core.HandlerResponse {

core.Log("Body recibido::", *args.Body)
offer := RtcClientOffer{}
err := json.Unmarshal([]byte(*args.Body), &offer)
if err != nil {
core.Log("Error al interpretar el mensaje:", err)
return core.HandlerResponse{}
}

if offer.Offer == "" {
core.Log("No se recibió el SPD Offer")
return core.HandlerResponse{}
}

offer.ClientID = args.ClientID

core.Print(offer)

dynamoTable := MakeClientTable()
dynamoTable.PutItem(&offer, 1)

return core.HandlerResponse{}
// Devuelve los ultimos usuarios conectados
query := aws.DynamoQueryParam{Index: "sk", GreaterThan: "0", ScanIndexForward: true, Limit: 50}
records, err := dynamoTable.QueryBatch([]aws.DynamoQueryParam{query})
if err != nil {
core.Log("Error al obtener las últimas conexiones:", err)
return core.HandlerResponse{}
}

core.Log("Número de conexiones obtenidas::", len(records))

return args.MakeResponse(records)
}
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func LocalWssHandler(w http.ResponseWriter, r *http.Request) {
core.Log("Recibido: ", string(message))
args := ParseWssMessage(message)
args.IsWebSocket = true
args.WebSocketConn = ws
mainHandler(args)
}
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "example-basic",
"type": "module",
"scripts": {
"dev": "vinxi dev",
"dev": "vinxi dev --port 3588",
"build": "vinxi build && node build.js",
"start": "vinxi start",
"start": "vinxi start --port 3588",
"version": "vinxi version"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/services/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ export class ConnectionManager {

async sendOffer(){
const offerString = await webRTCManager.getOffer()
await this.sendMessage("PostRtcOffer",offerString)
const message = { offer: offerString }
await this.sendMessage("PostRtcOffer", JSON.stringify(message))
}
}

Expand Down
23 changes: 23 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
if [[ "$OSTYPE" == "linux-gnu"* ]]; then # comando para LINUX

konsole & sleep 1

qdbus org.kde.konsole-$! /konsole/MainWindow_1 org.kde.KMainWindow.activateAction split-view-left-right

qdbus org.kde.konsole-$! /Windows/1 org.kde.konsole.Window.setCurrentSession 1
qdbus org.kde.konsole-$! /Sessions/1 setTitle 1 'FRONTEND'
qdbus org.kde.konsole-$! /Sessions/1 runCommand 'cd ./frontend && npm run dev'

qdbus org.kde.konsole-$! /Sessions/2 setTitle 1 'BACKEND'
qdbus org.kde.konsole-$! /Sessions/2 runCommand 'cd ./backend && air dev'

qdbus org.kde.konsole-$! /Windows/2 org.kde.konsole.Window.setCurrentSession 1
qdbus org.kde.konsole-$! /Sessions/0 setTitle 1 'GERP'

else # comando para WINDOWS

echo 'nada configurado'
echo 'probar cmder ==> https://www.eventslooped.com/posts/automate-open-tabs-in-cmder/'

fi

0 comments on commit 6dd70c9

Please sign in to comment.