fix: improve w2g

This commit is contained in:
ThaUnknown 2022-04-21 19:30:43 +02:00
parent e16cc976b8
commit 95ce41592f
4 changed files with 36 additions and 8 deletions

View file

@ -1,6 +1,6 @@
{
"name": "Miru",
"version": "1.7.5",
"version": "1.7.6",
"author": "ThaUnknown_ <ThaUnknown@users.noreply.github.com>",
"main": "src/index.js",
"homepage": "https://github.com/ThaUnknown/miru#readme",

View file

@ -1,10 +1,23 @@
<script>
import { addToast } from '@/lib/Toasts.svelte'
export let state
export let peer
export let cancel
let values = []
let code = ''
let timeout = null
function rej() {
addToast({
text: 'Could not establish connection, please try again.',
title: 'Connection Timed Out.',
type: 'danger'
})
cancel()
}
peer.signalingPort.onmessage = ({ data }) => {
console.log(data)
const { description, candidate } = typeof data === 'string' ? JSON.parse(data) : data
@ -12,16 +25,24 @@
if (description.type === 'answer') values = []
values.push(description.sdp)
} else if (candidate) {
values.push(candidate.candidate)
if (candidate.candidate.includes('srflx')) values.push(candidate.candidate)
}
if (values.length > 1) {
code = btoa(JSON.stringify(values))
clearTimeout(timeout)
}
code = btoa(JSON.stringify(values))
}
if (state === 'host') timeout = setTimeout(rej, 10000)
$: value = (step?.mode === 'copy' && code) || null
function handleInput({ target }) {
const val = JSON.parse(atob(target.value))
const [description, ...candidates] = val
if (state === 'guest') timeout = setTimeout(rej, 10000)
peer.signalingPort.postMessage({
description: {
type: state === 'host' ? 'answer' : 'offer',
@ -78,7 +99,7 @@
</script>
<div class="h-full d-flex flex-column justify-content-center mb-20 pb-20 root container">
{#if step}
{#if step && (value || step.mode === 'paste')}
<h1 class="font-weight-bold">
{step.title}
</h1>
@ -89,6 +110,8 @@
{#if step.mode === 'copy' && value}
<button class="btn btn-primary mt-5" type="button" on:click={copyData} disabled={!value}>Copy</button>
{/if}
{:else}
<h1 class="font-weight-bold">Connecting...</h1>
{/if}
<button class="btn btn-danger mt-5" type="button" on:click={cancel}>Cancel</button>
</div>

View file

@ -63,8 +63,10 @@
})
window.IPC.on('torrent', file => {
playerState.file = file
emit('torrent', { file })
if (!playerState.file.every((v, i) => v === file[i])) {
playerState.file = file
emit('torrent', { file })
}
})
function emit(type, data) {
@ -83,7 +85,7 @@
pending.update(peer => {
peer.pc.close()
})
state.set(null)
if (get(state) === 'guest') state.set(null)
}
function cleanup() {
@ -122,6 +124,7 @@
function handleMessage(data, channel, peer) {
console.log(data)
if (get(state) === 'host') emit(data.type, data)
switch (data.type) {
case 'init':
{

View file

@ -28,7 +28,9 @@ export default class Peer {
iceServers: options?.iceServers || [{
urls: [
'stun:stun.l.google.com:19302',
'stun:global.stun.twilio.com:3478'
'stun:stun1.l.google.com:19302',
'stun:global.stun.twilio.com:3478',
'stun:stun.nextcloud.com:443'
]
}]
})