chore: Update translations
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Martti Malmi 2023-12-21 17:59:56 +00:00
parent 1309937869
commit 782a2217b4
6 changed files with 74 additions and 63 deletions

View File

@ -1,6 +1,6 @@
import { NostrEvent, OkResponse, SystemInterface } from "@snort/system";
import { removeUndefined } from "@snort/shared";
import {getWebRtcPool} from "@/webrtc";
import { getWebRtcPool } from "@/webrtc";
export async function sendEventToRelays(
system: SystemInterface,

View File

@ -5,7 +5,11 @@ export class WebRTCConnection extends EventEmitter {
private peerConnection: RTCPeerConnection;
private dataChannel: RTCDataChannel;
constructor(private socket: Socket, configuration: RTCConfiguration, public peerId: string) {
constructor(
private socket: Socket,
configuration: RTCConfiguration,
public peerId: string,
) {
super();
this.peerConnection = new RTCPeerConnection(configuration);
this.dataChannel = this.peerConnection.createDataChannel("data");
@ -18,24 +22,24 @@ export class WebRTCConnection extends EventEmitter {
}
public async handleOffer(offer: RTCSessionDescriptionInit): Promise<void> {
this.log('Received offer', offer);
this.log("Received offer", offer);
await this.peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
await this.sendLocalDescription('answer');
await this.sendLocalDescription("answer");
}
public async handleAnswer(answer: RTCSessionDescriptionInit): Promise<void> {
this.log('Received answer', answer);
this.log("Received answer", answer);
await this.peerConnection.setRemoteDescription(new RTCSessionDescription(answer));
}
public handleCandidate(candidate: RTCIceCandidateInit): void {
this.log('Received ICE candidate', candidate);
this.log("Received ICE candidate", candidate);
this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
}
private async sendLocalDescription(type: 'offer' | 'answer'): Promise<void> {
private async sendLocalDescription(type: "offer" | "answer"): Promise<void> {
let description;
if (type === 'offer') {
if (type === "offer") {
description = await this.peerConnection.createOffer();
} else {
description = await this.peerConnection.createAnswer();
@ -46,19 +50,19 @@ export class WebRTCConnection extends EventEmitter {
}
private setupDataChannel(): void {
this.dataChannel.onopen = () => this.log('Data channel opened');
this.dataChannel.onclose = () => this.log('Data channel closed');
this.dataChannel.onopen = () => this.log("Data channel opened");
this.dataChannel.onclose = () => this.log("Data channel closed");
this.dataChannel.onmessage = event => this.handleDataChannelMessage(event);
}
private handleDataChannelMessage(event: MessageEvent): void {
this.log(`-> "${event.data}"`);
if (event.data === 'ping') {
this.send('pong');
if (event.data === "ping") {
this.send("pong");
} else {
try {
const data = JSON.parse(event.data);
this.emit('event', data);
this.emit("event", data);
} catch (e) {
// Ignore
}
@ -66,32 +70,32 @@ export class WebRTCConnection extends EventEmitter {
}
public send(data: any): void {
if (this.dataChannel.readyState === 'open') {
if (this.dataChannel.readyState === "open") {
this.log(`<- "${data}"`);
this.dataChannel.send(data);
}
}
public async handleHello(): Promise<void> {
if (this.peerConnection.connectionState === 'new') {
await this.sendLocalDescription('offer');
if (this.peerConnection.connectionState === "new") {
await this.sendLocalDescription("offer");
}
}
private registerPeerConnectionEvents(): void {
this.peerConnection.onicecandidate = event => {
if (event.candidate) {
this.log('Local ICE candidate:', event.candidate);
this.socket.emit('candidate', { candidate: event.candidate.toJSON(), recipient: this.peerId });
this.log("Local ICE candidate:", event.candidate);
this.socket.emit("candidate", { candidate: event.candidate.toJSON(), recipient: this.peerId });
}
};
this.peerConnection.oniceconnectionstatechange = () => {
this.log('ICE Connection State Change:', this.peerConnection.iceConnectionState);
this.log("ICE Connection State Change:", this.peerConnection.iceConnectionState);
};
this.peerConnection.onconnectionstatechange = () => {
this.log('WebRTC Connection State Change:', this.peerConnection.connectionState);
this.log("WebRTC Connection State Change:", this.peerConnection.connectionState);
};
this.peerConnection.ondatachannel = event => {

View File

@ -1,5 +1,5 @@
import {io, Socket} from 'socket.io-client';
import {WebRTCConnection} from '@/webrtc/WebRTCConnection';
import { io, Socket } from "socket.io-client";
import { WebRTCConnection } from "@/webrtc/WebRTCConnection";
import EventEmitter from "eventemitter3";
const MAX_CONNECTIONS = 5;
@ -19,14 +19,14 @@ class WebRTCPool extends EventEmitter {
}
private sayHello(): void {
this.signalingServer.emit('hello', this.peerId);
this.signalingServer.emit("hello", this.peerId);
}
public send(data: any, recipients?: string[]): void {
this.peers.forEach(conn => {
if (!recipients || recipients.includes(conn.peerId)) {
try {
conn.send(typeof data === 'string' ? data : JSON.stringify(data));
conn.send(typeof data === "string" ? data : JSON.stringify(data));
} catch (e) {
console.error(e);
}
@ -36,10 +36,10 @@ class WebRTCPool extends EventEmitter {
public createConnection(peerId: string): WebRTCConnection {
if (this.peers.size >= MAX_CONNECTIONS) {
throw new Error('Maximum connections reached');
throw new Error("Maximum connections reached");
}
const connection = new WebRTCConnection(this.signalingServer, this.configuration, peerId);
connection.on('event', (event: any) => this.emit('event', event));
connection.on("event", (event: any) => this.emit("event", event));
this.peers.set(peerId, connection);
return connection;
}
@ -51,31 +51,34 @@ class WebRTCPool extends EventEmitter {
}
private registerSocketEvents(): void {
this.signalingServer.on('connect', () => {
console.log('Connected to signaling server');
this.signalingServer.on("connect", () => {
console.log("Connected to signaling server");
this.sayHello();
});
this.signalingServer.on('offer', ({offer, sender}: { offer: RTCSessionDescriptionInit; sender: string }) => {
this.signalingServer.on("offer", ({ offer, sender }: { offer: RTCSessionDescriptionInit; sender: string }) => {
this.handleConnectionEvent(sender, async conn => await conn.handleOffer(offer));
});
this.signalingServer.on('answer', ({answer, sender}: { answer: RTCSessionDescriptionInit; sender: string }) => {
this.signalingServer.on("answer", ({ answer, sender }: { answer: RTCSessionDescriptionInit; sender: string }) => {
this.handleConnectionEvent(sender, async conn => await conn.handleAnswer(answer));
});
this.signalingServer.on('candidate', ({candidate, sender}: { candidate: RTCIceCandidateInit; sender: string }) => {
this.handleConnectionEvent(sender, conn => conn.handleCandidate(candidate));
});
this.signalingServer.on(
"candidate",
({ candidate, sender }: { candidate: RTCIceCandidateInit; sender: string }) => {
this.handleConnectionEvent(sender, conn => conn.handleCandidate(candidate));
},
);
this.signalingServer.on('hello', (sender: string) => {
console.log('Received hello from', sender);
this.signalingServer.on("hello", (sender: string) => {
console.log("Received hello from", sender);
this.handleConnectionEvent(sender, conn => conn.handleHello());
});
}
public close(): void {
console.log('closing pool');
console.log("closing pool");
this.signalingServer.close();
for (const conn of this.peers.values()) {
conn.close();
@ -83,4 +86,4 @@ class WebRTCPool extends EventEmitter {
}
}
export default WebRTCPool;
export default WebRTCPool;

View File

@ -1,4 +1,4 @@
import {LoginStore} from "@/Login";
import { LoginStore } from "@/Login";
import WebRTCPool from "@/webrtc/WebRTCPool";
let publicKey: string | undefined;
@ -9,13 +9,17 @@ LoginStore.hook(() => {
const login = LoginStore.takeSnapshot();
if (login.publicKey && !login.readonly && login.publicKey !== publicKey) {
publicKey = login.publicKey;
if (location.hostname === 'localhost') {
if (location.hostname === "localhost") {
pool?.close();
interval && clearInterval(interval);
pool = new WebRTCPool('http://localhost:3000', {
iceServers: [{ urls: 'stun:localhost:3478' }],
}, login.publicKey);
interval = setInterval(() => pool?.send('ping'), 10000);
pool = new WebRTCPool(
"http://localhost:3000",
{
iceServers: [{ urls: "stun:localhost:3478" }],
},
login.publicKey,
);
interval = setInterval(() => pool?.send("ping"), 10000);
}
}
});

View File

@ -5,4 +5,4 @@ yarn
yarn start
```
Websocket (socket.io) based signaling server for WebRTC.
Websocket (socket.io) based signaling server for WebRTC.

View File

@ -1,37 +1,37 @@
const PORT = process.env.PORT || 3000;
const io = require('socket.io')(PORT, {
const io = require("socket.io")(PORT, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
methods: ["GET", "POST"],
},
});
const peerSocketMap = new Map();
const socketPeerMap = new Map();
io.on('connection', socket => {
io.on("connection", socket => {
console.log(`New client connected, socket ID: ${socket.id}`);
const emitToTarget = (eventType, data, recipient) => {
const targetSocketId = peerSocketMap.get(recipient);
const sender = socketPeerMap.get(socket.id);
if (sender && targetSocketId) {
io.to(targetSocketId).emit(eventType, {...data, sender});
io.to(targetSocketId).emit(eventType, { ...data, sender });
}
};
socket.on('offer', data => emitToTarget('offer', data, data.recipient));
socket.on('answer', data => emitToTarget('answer', data, data.recipient));
socket.on('candidate', data => emitToTarget('candidate', data, data.recipient));
socket.on("offer", data => emitToTarget("offer", data, data.recipient));
socket.on("answer", data => emitToTarget("answer", data, data.recipient));
socket.on("candidate", data => emitToTarget("candidate", data, data.recipient));
socket.on('hello', peerId => {
socket.on("hello", peerId => {
console.log(`Received hello from ${peerId}`);
peerSocketMap.set(peerId, socket.id);
socketPeerMap.set(socket.id, peerId);
socket.broadcast.emit('hello', peerId);
socket.broadcast.emit("hello", peerId);
});
socket.on('disconnect', () => {
socket.on("disconnect", () => {
peerSocketMap.delete(socketPeerMap.get(socket.id));
socketPeerMap.delete(socket.id);
console.log(`Client disconnected, socket ID: ${socket.id}`);
@ -43,14 +43,14 @@ console.log(`Signaling server running on port ${PORT}`);
const Ministun = require("ministun");
const stunConfig = {
udp4: true,
udp6: true,
port: 3478,
log: console.log,
err: console.err,
sw: true
udp4: true,
udp6: true,
port: 3478,
log: console.log,
err: console.err,
sw: true,
};
const server = new Ministun(stunConfig);
console.log(`STUN server running on port ${stunConfig.port}`)
console.log(`STUN server running on port ${stunConfig.port}`);