简体   繁体   中英

Add event handler to LightStreamer

I want to scrape a website which uses LightStreamer for showing live data. How can i add an event handler to receive those messages in Javascript?
Unfortunately, LightStreamer doesn't have enough tutorial.
Following sample works for me:

var myEH = function ff(msg) {
            console.log("message receieved.");
        };
        
App.ls.getSubscriptions()[0].addListener({ onItemUpdate : myEH})

When i repalce "message receieved." with msg variable, i see a non-sense array which is not similar to what i see in Chrome DevTools (Network tab> ws section).

Also, there are isActive() and isSubscribed() method for each subscription. When i check them against array elements returned by getSubscriptions(), i see that all of them are true. But, in fact, Chrome DevTools shows that only one them that is active and receives message. Finding active subscription is another problem.

Update : Thanks @Robert for his help. Finally, i could understand that getSubscriptions()[6] includes what i need. Other subscriptions update clock or other things in page. I extracted items and fields from this subscription as following:

var myItems = App.ls.getSubscriptions()[6].getItems();
var myFields = App.ls.getSubscriptions()[6].getFields();
var myEH3 = function ff(msg) {
            console.log('tttt3'); 
        };
        
var mySub = new Subscription("MERGE", myItems, myFields);
mySub.addListener(myEH3);
App.ls.subscribe(mySub);

But this doesn't work. Server returns error 'Data Adapter Not found.' Changing to 'mySub.addListener({ onItemUpdate: myEH3});' does not help. I have already tried to add eventhandler directly to getSubscriptions()[6] via 'App.ls.getSubscriptions()[6].addListener({ onItemUpdate: myEH3});', but my function never called. Any Hint would be greatly apprecieated.

They have tutorial, and solid api documentation... from

<script>
  require(["LightstreamerClient", "Subscription", "StaticGrid"], function(LightstreamerClient, Subscription, StaticGrid) {
    var client = new LightstreamerClient(null, "HELLOWORLD");
    client.connect();
    // you have to create your own SubscriptionListener
    var grid = new StaticGrid("hellogrid", true);
    
    var subscription = new Subscription("MERGE", grid.extractItemList(), grid.extractFieldList());
    subscription.addListener(grid);
    
    client.subscribe(subscription);
  });      
</script>  

Now you have to create your own subscription listener according to follow interface. And then you have onItemUpdate on your subscription listener.

Finally, I could get my head around this library. Searching JavaScript files of webpage, I found that somewhere DataAdapter will be set to subscriptions.
This is my final code:

var myItems = App.ls.getSubscriptions()[6].getItems();
var myFields = App.ls.getSubscriptions()[6].getFields();
var adaptername = App.ls.getSubscriptions()[6].getDataAdapter();
var myEH3 = function ff(msg) {
            console.log('message received.'); 
        };
        
var mySub = new Subscription("MERGE", myItems, myFields);
mySub.setDataAdapter(adaptername);
mySub.setRequestedSnapshot("yes");

mySub.addListener({onItemUpdate: myEH3});
App.ls.subscribe(mySub);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM