ng2-stomp-service

快速使用:

Fast usage example

import { StompService } from 'ng2-stomp-service';
 
private subscription : any;
 
constructor(stomp: StompService) {
 
  //configuration 
  stomp.configure({
    host:'test.com',
    debug:true,
    queue:{'init':false}
  });
  
  //start connection 
  stomp.startConnect().then(() => {
    stomp.done('init');
    console.log('connected');
    
    //subscribe 
    this.subscription = stomp.subscribe('/destination', this.response});
    
    //send data 
    stomp.send('destionation',{"data":"data"});
    
    //unsubscribe 
    this.subscription.unsubscribe();
    
    //disconnect 
    stomp.disconnect().then(() => {
      console.log( 'Connection closed' )
    })
    
  });
 
 
}
 
//response 
public response = (data) => {
  console.log(data)
}

队列:

The Queue
When your application is going to scale the 'fast usage example' is not for you.. but it's helpful for beginning.

When you have routes and different actions in your application you will need to use 'queue' of subscriptions with after() and done() methods.

First create queue of subscriptions. The first one 'init' is required then we can add other queue for example 'user' and assign it as false.

  stomp.configure({
    host:'test.com',
    debug:true,
    queue:{'init':false,'user':false}
  });

When connection established we have to call done() methdod with 'init' parameter.

 stomp.startConnect().then(() => {
    stomp.done('init');
 })

Now we can use after() method in different components and classes. Which checks continuously if specified queue have been done.

  stomp.after('init').then(()=>{
    stomp.subscribe('user',(data)=>{
    
      //here we done our queue 
      stomp.done('user');
      
    })
  })

As you can see we subscribed 'user' destination when 'init' queue has been complated. As you understand you can done your queue list when you want.. If some component needs user information you have to use following code

  stomp.after('user').then(()=>{
    console.log('do something')
  })
posted @ 2018-04-15 18:33  伊吹斯特  阅读(174)  评论(0编辑  收藏  举报