- From: Martin Thomson <martin.thomson@gmail.com>
- Date: Fri, 31 Oct 2014 08:16:48 -0700
- To: "public-webrtc@w3.org" <public-webrtc@w3.org>
So a recap on the keys stuff: RTCKeys.generate(algorithm).then(key => { pc = new RTCPeerConnection({ dtlsKeys: [ key ] }); }); The key that you get can be stored in indexeddb, and I can't pretend that I like the API: it's awful. Most of this awfulness is because indexeddb is awful. It's so awful that I can't describe it. I've done this all inline, but obviously you can build a good wrapper that doesn't suck. Everyone does. ////// This is just setup let dbRequest = indexedDB.open("keys"); let dbResolve; let dbPromise = new Promise(r => dbResolve = r); request.onupgradeneeded = e => { let store = dbRequest.result.createObjectStore("peerKeys", {keyPath: "peerIdentity"}); dbResolve(dbRequest.result); }; request.onsuccess = e => { dbResolve(dbRequest.result); }; ////// this is to retrieve a key let userid = "john@example.com"; let retrievePromise = dbPromise.then(db => { return new Promise((resolve, reject) => { let tx = db.transaction("peerKeys", "readonly"); let store = tx.objectStore("peerKeys"); let request = store.get(userid); request.onsuccess = e => { if (e.target.result) { resolve(e.target.result.key); } else { reject(new Error("no stored key for " + userid)); } }; }); }) /////// if the storage fails, then generate .catch(e => { return RTCKeys.generate(algorithm).then(key => { // store the key for next time... return dbPromise.then(db => { let tx = db.transaction("peerKeys", "readwrite"); let store = tx.objectStore("peerKeys"); return new Promise(resolve => { let request = store.put({ peerIdentity: userid, key: key }); request.onsuccess = e => resolve(key); }); }); }); }) ////// finally use it .then(key => { pc = new RTCPeerConnection({ dtlsKeys: [ key ] }); }); Promises are awesome.
Received on Friday, 31 October 2014 15:17:16 UTC