Skip to content

Commit

Permalink
[@dhealthdapps/frontend] feat(widgets): trying to enable connection w…
Browse files Browse the repository at this point in the history
…ith backend
  • Loading branch information
kravchenkodhealth authored and evias committed Jan 3, 2023
1 parent 9bc846c commit c0d9a33
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
36 changes: 30 additions & 6 deletions runtime/backend/src/common/gateways/AuthGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,43 @@
* @license LGPL-3.0
*/
// external dependencies
import { SubscribeMessage, WebSocketGateway } from "@nestjs/websockets";
import { BaseGateway } from "./BaseGateway";
import {
SubscribeMessage,
WebSocketGateway,
MessageBody,
WebSocketServer,
OnGatewayConnection,
OnGatewayInit,
OnGatewayDisconnect,
} from "@nestjs/websockets";
import { IncomingMessage, ServerResponse } from "http";
import { Server } from "https";

// internal dependencies
import { BaseGateway } from "./BaseGateway";
import dappConfigLoader from "../../../config/dapp";

const dappConfig = dappConfigLoader();
@WebSocketGateway({
@WebSocketGateway(80, {
namespace: `${dappConfig.dappName}`,
cors: {
origin: process.env.FRONTEND_URL,
},
})
export class AuthGateway extends BaseGateway {
export class AuthGateway
extends BaseGateway
implements
OnGatewayConnection,
OnGatewayInit,
OnGatewayDisconnect
{
@WebSocketServer()
protected server: Server;

@SubscribeMessage("auth.open")
open(message: any) {
handleEvent(@MessageBody() message: any) {
console.log("AUTHGATEWAY: Connection open");
return { msg: "You're connected" };
}

@SubscribeMessage("auth.close")
Expand All @@ -40,10 +60,14 @@ export class AuthGateway extends BaseGateway {
handleConnection(
server: Server<typeof IncomingMessage, typeof ServerResponse>,
client: any,
): void {
) {
console.log("FRONTEND CLIENT CONNECTED");
}

handleDisconnect(client: any) {
console.log("BASEGATEWAY: Client disconnected");
}

afterInit(server: Server) {
console.log("AUTHGATEWAY INITIALIZED");
}
Expand Down
7 changes: 5 additions & 2 deletions runtime/backend/src/common/gateways/BaseGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
OnGatewayConnection,
OnGatewayDisconnect,
OnGatewayInit,
SubscribeMessage,
MessageBody,
} from "@nestjs/websockets";
import { Server } from "https";

Expand All @@ -32,14 +34,15 @@ const dappConfig = dappConfigLoader();
export abstract class BaseGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
{
@WebSocketServer()
protected server: Server;
// @WebSocketServer()
// protected server: Server;

protected clients: string[];

handleConnection(server: Server, client: any) {
console.log("BASEGATEWAY: Client connected");
this.clients.push(client.challenge);
console.log({ clients: this.clients });
}

handleDisconnect(client: any) {
Expand Down
1 change: 0 additions & 1 deletion runtime/backend/src/common/modules/AuthModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const auth = securityConfigLoader().auth;
*/
@Module({
imports: [
AuthGateway,
NetworkModule,
QueryModule,
AccountsModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,16 @@ export default class LoginScreen extends MetaView {
public async mounted() {
this.qrConfig = this.createLoginQRCode();

this.wsConnection = io("http://localhost:7903/");
this.wsConnection = io("http://localhost:80/");

this.wsConnection.on("connect", () => {
console.log("Successfully connected to the echo websocket server...");
});

// this.wsConnection.send("auth.open", JSON.stringify({ val: "test" }));
this.wsConnection.emit("auth.open");
this.wsConnection.emit("auth.open", { data: "test msg" }, (res: any) => {
console.log({ res });
});

try {
// make sure referral code is saved
Expand Down

0 comments on commit c0d9a33

Please sign in to comment.