-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.ts
68 lines (58 loc) · 2.03 KB
/
sender.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { init, Ditto, Logger, TransportConfig } from '@dittolive/ditto'
require('dotenv').config()
Logger.minimumLogLevel = 'Info'
async function main() {
// Initialize the Ditto module
await init()
let ditto = new Ditto({
type: 'onlinePlayground',
appID: process.env.APP_ID,
token: process.env.APP_TOKEN,
enableDittoCloudSync: false
})
const config = new TransportConfig()
config.peerToPeer.bluetoothLE.isEnabled = true
config.peerToPeer.lan.isEnabled = true
config.peerToPeer.awdl.isEnabled = false
ditto.observeTransportConditions((condition, source) => {
if (condition === 'BLEDisabled') {
console.log('BLE disabled')
} else if (condition === 'NoBLECentralPermission') {
console.log('Permission missing for BLE')
} else if (condition === 'NoBLEPeripheralPermission') {
console.log('Permissions missing for BLE')
}
})
ditto.setTransportConfig(config)
ditto.startSync()
let modelCollection = ditto.store.collection("models")
let modelSubscription = modelCollection.find("ACK == true").subscribe()
modelCollection.find("ACK == true").observeLocal((docs, event) => {
// new docs
for (let i = 0; i < docs.length; i++) {
let doc = docs[i]
Logger.info(`attachment delivery ACKed: ${doc.id}`)
}
})
const attachmentPath = "./"
const attachmentFilename = "rp.jpg"
const metadata = { model: 'tester', source: attachmentPath + attachmentFilename}
const attachment = await modelCollection.newAttachment(attachmentPath + attachmentFilename, metadata)
const docID = await modelCollection.upsert({
ACK: false,
model: 'tester',
source: attachmentPath,
filename: attachmentFilename,
target: './ditto',
my_attachment: attachment
})
console.log("Upserted attachment: ", docID)
let presence = ditto.presence.observe((graph) => {
if (graph.remotePeers.length != 0) {
graph.remotePeers.forEach((peer) => {
console.log("peer connection: ", peer.deviceName, peer.connections[0].connectionType)
})
}
})
}
main()