You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Peer connection is being handled in this library, if you intended to use existing connection, contribute to this library.
This is built on top of typescript, and commonjs module is compulsory.
import{Sender,Receiver,humanFileSize}from"webshare";constlabelInfo=document.getElementById("label:info")!;constlabelError=document.getElementById("label:error")!;constbtnSend=document.getElementById("btn:send")!;constbtnReceive=document.getElementById("btn:receive")!;constinput=document.getElementById("input:file")asHTMLInputElement;constlabelProgress=document.getElementById("label:progress")!;constlabelRate=document.getElementById("label:rate")!;constpeerConfig={host: "192.168.43.155",port: 4112};
let file: File;input.addEventListener("change",(e)=>{if(!input.files){return;}file=input.files[0];sendfile(input.files[0]);});constgenerateId=()=>(newDate()).getTime().toString();
let sender: Sender;constsendfile=(file: File)=>{constid="sender";sender=newSender(peerConfig,file,id);sender.on("open",(id)=>{labelInfo.innerText=`Connection opened at ${id}`});sender.on("connected",connection=>{labelInfo.innerText=`Connection with connection id ${connection.label}`;});sender.on("disconnected",()=>{labelInfo.innerText=`Disconnected`;});sender.on("error",err=>{labelError.innerText=err.message;});sender.on("transferrate",rate=>{labelRate.innerText=humanFileSize(rate);});sender.on("progress",(f,byte)=>{labelProgress.innerText=`${humanFileSize(byte)} / ${humanFileSize(f.size)}`;})sender.on("completed",()=>{labelInfo.innerText="File has been sent successfully";})}
let receiver: Receiver;constreceiveFile=()=>{constid=prompt("Enter connection id");if(!id){labelError.innerText="Connection id is required";return;}receiver=newReceiver(peerConfig,id);receiver.on("open",rId=>{labelInfo.innerText=`Receiver is open on ${rId}`;});receiver.on("connected",connection=>{labelInfo.innerText=`Receiver is connected ${connection.label}`;labelError.innerText="";});receiver.on("disconnected",()=>{labelError.innerText=`Connection disconnected`;});receiver.on("close",()=>{labelError.innerText="Closed connection";})receiver.on("error",err=>{labelError.innerText=err.message;});receiver.on("progress",(f,byte)=>{labelProgress.innerText=`${humanFileSize(byte)} / ${humanFileSize(f.size)}`;})receiver.on("completed",file=>{console.log(file)receiver.download(file);});receiver.on("transferrate",byte=>{labelRate.innerText=humanFileSize(byte);});}consthandleClose=()=>{receiver&&receiver.close();sender&&sender.close();}btnReceive.addEventListener("click",receiveFile);document.getElementById("btn:close")!.addEventListener("click",handleClose);``