chore: Update translations

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 { NostrEvent, OkResponse, SystemInterface } from "@snort/system";
import { removeUndefined } from "@snort/shared"; import { removeUndefined } from "@snort/shared";
import {getWebRtcPool} from "@/webrtc"; import { getWebRtcPool } from "@/webrtc";
export async function sendEventToRelays( export async function sendEventToRelays(
system: SystemInterface, system: SystemInterface,

View File

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

View File

@ -1,5 +1,5 @@
import {io, Socket} from 'socket.io-client'; import { io, Socket } from "socket.io-client";
import {WebRTCConnection} from '@/webrtc/WebRTCConnection'; import { WebRTCConnection } from "@/webrtc/WebRTCConnection";
import EventEmitter from "eventemitter3"; import EventEmitter from "eventemitter3";
const MAX_CONNECTIONS = 5; const MAX_CONNECTIONS = 5;
@ -19,14 +19,14 @@ class WebRTCPool extends EventEmitter {
} }
private sayHello(): void { private sayHello(): void {
this.signalingServer.emit('hello', this.peerId); this.signalingServer.emit("hello", this.peerId);
} }
public send(data: any, recipients?: string[]): void { public send(data: any, recipients?: string[]): void {
this.peers.forEach(conn => { this.peers.forEach(conn => {
if (!recipients || recipients.includes(conn.peerId)) { if (!recipients || recipients.includes(conn.peerId)) {
try { try {
conn.send(typeof data === 'string' ? data : JSON.stringify(data)); conn.send(typeof data === "string" ? data : JSON.stringify(data));
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
@ -36,10 +36,10 @@ class WebRTCPool extends EventEmitter {
public createConnection(peerId: string): WebRTCConnection { public createConnection(peerId: string): WebRTCConnection {
if (this.peers.size >= MAX_CONNECTIONS) { 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); 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); this.peers.set(peerId, connection);
return connection; return connection;
} }
@ -51,31 +51,34 @@ class WebRTCPool extends EventEmitter {
} }
private registerSocketEvents(): void { private registerSocketEvents(): void {
this.signalingServer.on('connect', () => { this.signalingServer.on("connect", () => {
console.log('Connected to signaling server'); console.log("Connected to signaling server");
this.sayHello(); 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.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.handleConnectionEvent(sender, async conn => await conn.handleAnswer(answer));
}); });
this.signalingServer.on('candidate', ({candidate, sender}: { candidate: RTCIceCandidateInit; sender: string }) => { this.signalingServer.on(
this.handleConnectionEvent(sender, conn => conn.handleCandidate(candidate)); "candidate",
}); ({ candidate, sender }: { candidate: RTCIceCandidateInit; sender: string }) => {
this.handleConnectionEvent(sender, conn => conn.handleCandidate(candidate));
},
);
this.signalingServer.on('hello', (sender: string) => { this.signalingServer.on("hello", (sender: string) => {
console.log('Received hello from', sender); console.log("Received hello from", sender);
this.handleConnectionEvent(sender, conn => conn.handleHello()); this.handleConnectionEvent(sender, conn => conn.handleHello());
}); });
} }
public close(): void { public close(): void {
console.log('closing pool'); console.log("closing pool");
this.signalingServer.close(); this.signalingServer.close();
for (const conn of this.peers.values()) { for (const conn of this.peers.values()) {
conn.close(); 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"; import WebRTCPool from "@/webrtc/WebRTCPool";
let publicKey: string | undefined; let publicKey: string | undefined;
@ -9,13 +9,17 @@ LoginStore.hook(() => {
const login = LoginStore.takeSnapshot(); const login = LoginStore.takeSnapshot();
if (login.publicKey && !login.readonly && login.publicKey !== publicKey) { if (login.publicKey && !login.readonly && login.publicKey !== publicKey) {
publicKey = login.publicKey; publicKey = login.publicKey;
if (location.hostname === 'localhost') { if (location.hostname === "localhost") {
pool?.close(); pool?.close();
interval && clearInterval(interval); interval && clearInterval(interval);
pool = new WebRTCPool('http://localhost:3000', { pool = new WebRTCPool(
iceServers: [{ urls: 'stun:localhost:3478' }], "http://localhost:3000",
}, login.publicKey); {
interval = setInterval(() => pool?.send('ping'), 10000); iceServers: [{ urls: "stun:localhost:3478" }],
},
login.publicKey,
);
interval = setInterval(() => pool?.send("ping"), 10000);
} }
} }
}); });

View File

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