2017-12-31 141 views
0

자바 스크립트에서 웹 소켓을 사용하여 API에 연결하고 일부 데이터를 읽습니다.ws.on과 ws.onmessage 사이의 웹 소켓 차이

function processHUOBIAPI(client,exchange_name,exchange_wss,exchange_symbol) 
 
{ 
 
    // Define constants 
 
    const WebSocket = require('ws'); 
 
    const pako = require('pako'); 
 

 
    // Open connection once one is established 
 
    var wss = new WebSocket(exchange_wss); 
 

 
    wss.onopen =() => 
 
    { 
 
    // Send request to subscribe 
 
    var symbol = exchange_symbol.toLowerCase() 
 
    wss.send(JSON.stringify(
 
     { 
 
     "sub": "market." + symbol + ".kline.1min", 
 
     "id": symbol 
 
     } 
 
    )); 
 
    }; 
 

 
    // Parse channel information and send to Redis 
 
    wss.onmessage = (data) => 
 
    { 
 
    //typedef data; 
 
    console.log("Receive message", data); 
 
    let text = pako.inflate(data, { 
 
     to: 'string' 
 
    }); 
 
    mssg = JSON.parse(text); 
 
    if (mssg.ping) { 
 
     wss.send(JSON.stringify({ 
 
      pong: mssg.ping 
 
     })); 
 
    } else if (mssg.tick) { 
 
     console.log(mssg); 
 
     //handle(msg); 
 
    } else { 
 
     console.log(text); 
 
    } 
 
    }

이 경우 나는 오류가 발생 : 여기

코드입니다

.../node_modules/파코/lib 디렉토리/inflate.js : 384 if (inflator.err) {throw inflator.msg || msg [inflator.err]; I는 바꾸면} 는^ 알려지지 압축 방법

"wss.onmessage = (데이터) =>" "wss.on ('메시지'(데이터) => {" 및 로 삭제 추가 ') 코드가 완벽하게 작동합니다.

wss.on과 wss.onmessage의 차이점은 무엇입니까?

새내기는이 안내서를 참고할 것입니다. 어리석은 사람이라면 미리 사과드립니다 ....

답변

0

WSWebSocket API에 대한 래퍼이므로 "onmessage"는 여기서 작동하지 않습니다.