add processing of pushed packets. The _header.id prop but be set to 'pushed' instead of id of the packet generated by send

master
David Kebler 2018-05-24 12:17:56 -07:00
parent b6be936f44
commit 7b7872cb64
2 changed files with 10 additions and 2 deletions

View File

@ -39,6 +39,14 @@ class Consumer extends EventEmitter {
listen (func) {
this.socket.addEventListener('message', handler.bind(this))
this.on('pushed',async function(packet){
// TODO do some extra security here?
let res = await this._packetProcess(packet)
if (!res) { // if process was not promise returning like just logged to console
console.log('warning: consumer process function was not promise returning')
}
})
function handler (event) {
let packet = {}
if (this.socket.readyState === 1) {
@ -67,7 +75,6 @@ class Consumer extends EventEmitter {
this.socket.send(message)
// listen for when packet comes back with unique header id
this.once(packet._header.id, async function (reply) {
// console.log('reply emitted', reply)
let res = await this._packetProcess(reply)
if (!res) { // if process was not promise returning like just logged to console
res = reply
@ -86,6 +93,7 @@ class Consumer extends EventEmitter {
async _packetProcess (packet) {
return Promise.resolve(packet)
}
} // end Consumer Class

View File

@ -1,6 +1,6 @@
{
"name": "@uci/websocket-client",
"version": "0.1.2",
"version": "0.1.3",
"description": "JSON packet browser client over web socket",
"main": "browser-client",
"scripts": {