-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6235b0
commit d1cd3a4
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Emittery from 'emittery'; | ||
import pEvent from 'p-event'; | ||
import readBlob from 'read-blob'; | ||
|
||
class MarketmakerSocket { | ||
constructor(endpoint) { | ||
this._ws = new WebSocket(endpoint, ['pair.sp.nanomsg.org']); | ||
this.connected = pEvent(this._ws, 'open'); | ||
|
||
const ee = new Emittery(); | ||
this._ee = ee; | ||
this.on = ee.on.bind(ee); | ||
this.off = ee.off.bind(ee); | ||
this.once = ee.once.bind(ee); | ||
|
||
this._ws.addEventListener('message', this._handleMessage); | ||
} | ||
|
||
_handleMessage = async event => { | ||
const json = await readBlob.text(event.data); | ||
const data = JSON.parse(json); | ||
const queueId = data.queueid; | ||
const message = data.result; | ||
|
||
if (queueId > 0) { | ||
this._ee.emit(`id_${queueId}`, message); | ||
} | ||
|
||
this._ee.emit('message', message); | ||
} | ||
|
||
getResponse = queueId => this._ee.once(`id_${queueId}`); | ||
} | ||
|
||
export default MarketmakerSocket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters