项目作者: subins2000

项目描述 :
A library to send files over WebRTC
高级语言: TypeScript
项目地址: git://github.com/subins2000/simple-peer-files.git
创建时间: 2020-08-09T22:02:44Z
项目社区:https://github.com/subins2000/simple-peer-files

开源协议:Mozilla Public License 2.0

下载


WebRTC Simple File Transfer

A simple library to send & receive files over WebRTC data channels. All you need to pass is a simple-peer object, the file, and an ID!

Features

  • Pause/Resume file transfers
  • No file size limit
  • Independent, just pass a simple-peer object
  • Multiple file transfers at the same time using the same simple-peer object

Examples

Apps Made With SPF

Simple Example

Open this webpage in two separate browser windows. This simple example is based on the example shown in simple-peer README

Sender :

  1. import SimplePeerFiles from 'simple-peer-files'
  2. const spf = new SimplePeerFiles()
  3. function readyToSend () {
  4. // peer is the SimplePeer object connection to receiver
  5. spf.send(peer, 'myFileID', file).then(transfer => {
  6. transfer.on('progress', sentBytes => {
  7. console.log(sentBytes)
  8. })
  9. transfer.start()
  10. })
  11. }

Receiver :

  1. import SimplePeerFiles from 'simple-peer-files'
  2. const spf = new SimplePeerFiles()
  3. // peer is the SimplePeer object connection to sender
  4. spf.receive(peer, 'myFileID').then(transfer => {
  5. transfer.on('progress', sentBytes => {
  6. console.log(sentBytes)
  7. })
  8. // Call readyToSend() in the sender side
  9. peer.send('heySenderYouCanSendNow')
  10. })

You have to call spf.receive() in receiver before you call spf.send() in sender. This is to prepare the receiver to accept file before sending starts. This also allows to implement a functionality for the receiver to accept or reject the file.

Thanks to Andrew Bastin’s justshare for being a reference in making this library.