Guest User

Untitled

a guest
Aug 16th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
  2.  
  3. private messageSubject$ = new BehaviorSubject<string>('default message');
  4.  
  5. // Connecting to WebSocket found in Spring Boot MS. Also checkout polyfill.ts
  6. connect(user: string, password: string) {
  7. console.log('Inside Connect. Guest and Password: ' + user + ' ' + password);
  8. const socket = new SockJs('http://localhost:8080/football-ws');
  9. this.stompClient = Stomp.over(socket);
  10. this.stompClient.connect(user, password, this.onConnected);
  11. console.log('Attempting to connect!');
  12. }
  13.  
  14. onConnected = (frame: any) => {
  15. console.log('Frame: ' + frame);
  16. this.stompClient.subscribe('/topic/public', this.onMessageReceived);
  17. }
  18.  
  19.  
  20. onMessageReceived(message: any) {
  21. console.log('Message Received from MS: ' + message);
  22. try {
  23. this.messageSubject$.next(message.body);
  24. } catch (e) {
  25. console.log('Error: ' + e);
  26. }
  27. }
  28.  
  29. getMessageSubject() {
  30. return this.messageSubject$.asObservable();
  31. }
  32.  
  33. this.stompClient.connect(user, password, (frame: any) => this.onConnected(frame));
  34.  
  35. this.stompClient.subscribe('/topic/public', (message: any) => this.onMessageReceived(message));
  36.  
  37. this.stompClient.connect(user, password, (frame: any) => {
  38. console.log('Frame: ' + frame);
  39. this.stompClient.subscribe('/topic/public', (message: any) => {
  40. console.log('Message Received from MS: ' + message);
  41. try {
  42. this.messageSubject$.next(message.body);
  43. } catch (e) {
  44. console.log('Error: ' + e);
  45. }
  46. });
  47. });
Add Comment
Please, Sign In to add comment