node.jswebsocketwslightstreamer

use Lightstreamer Server with Node WebSocket only as Client?


Hello for some reasons i don't like to use the libs from Lightstreamer for example debugging with Charles Webproxy or sending raw text commands.

I get a connection to the server, but i fail after sending any command.

I used this https://demos.lightstreamer.com/HelloWorld/ and try make me a Node/TS version of it.

I send from the example the first string which is "wsok" and all what i get from server is (1011) Cannot continue due to an unexpected error.

My problem must be related to sending the message because without the connection is work.

http://192.168.185.24:8888 is my Charles Webdebugging.

import * as WebSocket from 'ws';
var url = require('url');
var HttpsProxyAgent = require('https-proxy-agent');

class sg{

    ws:WebSocket;
    constructor(){
        var headers = {};
        headers["Host"] ="push.lightstreamer.com";
        headers["User-Agent"] ="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0";
        headers["Accept"] ="*/*";
        headers["Accept-Language"] ="de,en-US;q=0.7,en;q=0.3";
        headers["Accept-Encoding"] ="gzip, deflate, br";
        headers["Sec-WebSocket-Version"] ="13";
        headers["Origin"] ="https://demos.lightstreamer.com";
        headers["Sec-WebSocket-Protocol"] ="TLCP-2.2.0.lightstreamer.com";
        headers["Sec-WebSocket-Extensions"] ="permessage-deflate";
        headers["Sec-WebSocket-Key"] ="a key==";
        headers["Connection"] ="keep-alive, Upgrade";
        headers["Sec-Fetch-Dest"] ="websocket";
        headers["Sec-Fetch-Mode"] ="websocket";
        headers["Sec-Fetch-Site"] ="same-site";
        headers["Pragma"] ="no-cache";
        headers["Cache-Control"] ="no-cache";
        headers["Upgrade"] ="websocket";


        var options = url.parse('http://192.168.185.24:8888');
        var agent = new HttpsProxyAgent(options);
        this.ws = new WebSocket("wss://push.lightstreamer.com/lightstreamer", ["js.lightstreamer.com", "websocket"], {
            headers, agent: agent,  rejectUnauthorized: false
        });
        this.ws.onopen = (e) => {
            console.log("open")
            setTimeout(() => {
                this.ws.send(`wsok`);
            }, 1500);
        };
        this.ws.onmessage = (e) => {
            console.log(e)
        }
        this.ws.onerror = error => {
            console.log(`WebSocket error: ${error}`)
        }
    }

}

let xsg = new sg();

Solution

  • You are not using the object WebSocket correctly.

    Try this instead:

    ws = new WebSocket("wss://push.lightstreamer.com/lightstreamer", "TLCP-2.2.0.lightstreamer.com")
    ws.onopen = (e) => {
      console.log("open")          
      ws.send("wsok")         
    }
    

    For the rest, you must look at the TLCP Specification document, the section Hands on in particular.