未在WebSocket响应Node.js上解决承诺

时间:2018-07-26 14:47:04

标签: node.js websocket

我正在尝试使用websocket as promised

在Websocket上做一个简单的请求-响应模式

  const start = _ => {
    const sb_client = require('./sb_websocket_client.js')(ln);
    const uuidv1 = require('uuid/v1');
    const wsUrl = "ws://localhost:8072/nfl/v0"; 
    //const wsUrl = "wss://test.api.suredbits.com/nfl/v0"; 
    const client = new W3CWebSocket(wsUrl);
    const wsp = new WebSocketAsPromised(wsUrl, {
      createWebSocket: url => new W3CWebSocket(url),
      packMessage: data => {
         const stringify = JSON.stringify(data);
         console.log("packMessage stringify " + stringify);
         return stringify;
       },
      unpackMessage: message => {
        const parse = JSON.parse(message);
        console.log("unpackMessage message " + message);
        return parse;
      },
      attachRequestId: data => { 
        const id = uuidv1();
        const result = Object.assign({uuid: id}, data) // attach requestId to message as `id` field
        return result;
      },
      extractRequestId: data => {
        const extractedUUID = data.uuid
        console.log("\nEXTRACTED UUID " + extractedUUID + "\n");
        return data && data.uuid;
      },   // read requestId from message `id` field
    });

    wsp.open()
    .then(() => wsp.sendRequest({channel : "info"}))
    .then(msg => console.log("\nreturned type: " + msg + "\n"));

我当前正在使用uuid发送websocket消息,然后使用相同的uuid接收消息,但是此处的承诺.then(msg => console.log(...))并未得到解决。

这是示例中当前输出的样子:

packMessage stringify {"uuid":"cb5a8a70-90e1-11e8-835f-a5b6fe02ed30","channel":"info"}
unpackMessage message {"message":"Welcome to the SuredBits API!","ln_uri":"0338f57e4e20abf4d5c86b71b59e995ce4378e373b021a7b6f41dabb42d3aad069@ln.test.suredbits.com","version":0,"openChannels":["info","games","players","stats","team"]}


unpackMessage message {"uuid":"cb5a8a70-90e1-11e8-835f-a5b6fe02ed30","invoice":"lnbcrt10n1pd4nh2gpp5segwnwrveepvu8e4vuxcfu0tct6ckwec2wwvzpqn33m88qz2nj0sdqqxqrrsse3zllm3e2k2pwgf2m03k64z30gykwlcp7k7v7vlh7eg4cf69alzj8f0yems672jlhyq038p04edj66upwevkvrwwqnn2hqv2948kmfgq879j00"}

EXTRACTED UUID cb5a8a70-90e1-11e8-835f-a5b6fe02ed30

0 个答案:

没有答案