Skip to content

Commit

Permalink
fix lambda wss handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjoz committed Jun 6, 2024
1 parent ce51504 commit 81c3400
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
30 changes: 15 additions & 15 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ func LambdaHandler(_ context.Context, request *core.APIGatewayV2HTTPRequest) (*e
core.Log("*body enviado: ", core.StrCut(request.Body, 400))
}

route := request.RequestContext.HTTP.Path
if len(route) == 0 {
route = request.RawPath
}
if len(route) == 0 {
core.Log("No custom path given, but AWS routed this request to this Lambda anyways.")
route = "MISSING"
}

args := core.HandlerArgs{
Body: &request.Body,
Query: request.QueryStringParameters,
Headers: request.Headers,
Route: route,
Route: request.RequestContext.HTTP.Path,
Method: request.RequestContext.HTTP.Method,
EventType: request.RequestContext.EventType,
ConnectionID: request.RequestContext.ConnectionID,
IsWebSocket: true,
}

wssEvents := []string{"CONNECT", "DISCONNECT", "MESSAGE"}
fmt.Println("Event type: ", args.EventType)
response := core.MainResponse{}
// Revisa si es websocket
if core.Contains(wssEvents, args.EventType) {
if args.EventType == "MESSAGE" {
args.Body = &request.Body
}
response = WssHandler(args)
awsArgs := ParseWssMessage([]byte(request.Body))
args.Body = awsArgs.Body
args.Route = awsArgs.Route
args.ClientID = awsArgs.ClientID
} else {
response = mainHandler(args)
if len(args.Route) == 0 {
args.Route = request.RawPath
}
if len(args.Route) == 0 {
core.Log("No path given, but AWS routed this request anyways.")
args.Route = "MISSING"
}
}

response = mainHandler(args)
core.Log("Retornano StatusCode: ", response.LambdaResponse.StatusCode, "|", response.LambdaResponse.IsBase64Encoded, "|", response.LambdaResponse.Body)

return &events.APIGatewayProxyResponse{
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"scripts": {
"dev": "vinxi dev --port 3588",
"build": "vinxi build && node build.js",
"build": "vinxi build",
"start": "vinxi start --port 3588",
"version": "vinxi version"
},
Expand Down
16 changes: 14 additions & 2 deletions frontend/public/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ setInterval(() => {
console.log('worker.js is running');
},1000)

const ws = new WebSocket('ws://127.0.0.1:3589/ws')
console.log('websocket::',ws)
console.log(self.location)

const getWssAPI = () => {
console.log(self.location.hostname === "0.0.0.0", !self.location.port)
if(self.location.hostname === "0.0.0.0" || !self.location.port){
console.log("retornando wss://pv5s7gfoge.execute-api.us-east-1.amazonaws.com/p/")
return "wss://pv5s7gfoge.execute-api.us-east-1.amazonaws.com/p/"
} else {
console.log("retornando ws://127...")
return "ws://127.0.0.1:3589/ws"
}
}

const ws = new WebSocket(getWssAPI())

onmessage = function(e) {

Expand Down
12 changes: 12 additions & 0 deletions frontend/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
"use client";
import { MetaProvider, Title } from "@solidjs/meta";
import { Router } from "@solidjs/router";
import { FileRoutes } from "@solidjs/start/router";
import { Suspense } from "solid-js";
import "@styles/global.css";

export const GetWssAPI = () => {
if(typeof window === 'undefined'){
return ""
}
if(window.location.hostname === "0.0.0.0" || !window.location.port){
return "wss://pv5s7gfoge.execute-api.us-east-1.amazonaws.com/p/"
} else {
return "ws://127.0.0.1:3589/ws"
}
}

export default function App() {

const myWorker = new Worker("worker.js");
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/services/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Dexie from 'dexie'
import { createSignal } from 'solid-js'
import { GetWssAPI } from '~/app'

let dexieInitPromise: Promise<void>
let dexiedb: Dexie
Expand Down Expand Up @@ -146,7 +147,7 @@ export class ConnectionManager {
onMessage: (e: any) => void

constructor(){
this.ws = new WebSocket('ws://127.0.0.1:3589/ws')
this.ws = new WebSocket(GetWssAPI())

this.onOpenPromise = Promise.all([
new Promise<void>((resolve) => {
Expand Down Expand Up @@ -211,8 +212,8 @@ export class ConnectionManager {
if(this.onOpenPromise){ await this.onOpenPromise }
console.log('Client-ID a enviar::', this.clientID)
const message = { a: accion, b: messageBody, c: this.clientID }
const array8int = await compressStringWithGzip(JSON.stringify(message))
this.ws.send(array8int)
// const array8int = await compressStringWithGzip(JSON.stringify(message))
this.ws.send(JSON.stringify(message))
}

async sendOffer(){
Expand Down

0 comments on commit 81c3400

Please sign in to comment.