角套接字侦听器无法通过观察

时间:2019-03-08 12:11:25

标签: angular websocket rxjs

我正在使用web socket在我们的Web门户上吸引听众。但是,我没有通过observable得到一些听众。

这是我的ChatService文件。这是我的示例代码。

Import {Injectable} from '@angular/core';
import { StorageService } from '@app/services';
import { BehaviorSubject, Observable } from 'rxjs';
import * as io from 'socket.io-client';

import { WSUrl } from './../app.constants';

@Injectable({
  providedIn: 'root'
})
export class ChatService {
  private url = WSUrl;
  private socket;

  private jobStop = new BehaviorSubject(null);
  private jobStart = new BehaviorSubject(null);
  private jobStatus = new BehaviorSubject(null);
  private jobVerification = new BehaviorSubject(null);
  public jobStop$ = this.jobStop.asObservable();
  public jobStart$ = this.jobStart.asObservable();
  public jobStatus$ = this.jobStatus.asObservable();
  public jobVerification$ = this.jobVerification.asObservable();

  constructor(private toastr: ToastrService, private storage: StorageService) {
    // Get access token from local storage
    const token = this.storage.getItem<string>('token');

    // For socket connection
    this.socket = io(this.url, {
      query: 'token=' + token
    });

    // Socket connection successfully made
    this.socket.on('connect', () => {
      console.log('Connection Made.');
      this.socket.emit(
        'join',
        {
          login_token: token
        },
        data => {
          console.log('Socket Join.');
          if (data.code === 400) {
            return false;
          }
        }
      );

      this.socket.on('job_status', data => {
        this.jobStatus.next(data);
      });

      this.socket.on('start_job_hirer', data => {
        this.jobStart.next(data);
      });

      this.socket.on('stop_job_worker', data => {
        this.jobStop.next(data);
      });

      this.socket.on('job_verification_hirer', data => {
        this.jobVerification.next(data);
      });
    });
  }

  // Track job
  public trackJob(job_id) {
    return Observable.create(observer => {
      this.socket.emit('track_job', { job_id }, data => {
        observer.next(data);
      });
    });
  }

  public stopJob(job_id) {
    return Observable.create(observer => {
      this.socket.emit('stop_job', { job_id: job_id }, data => {
        observer.next(data);
      });
    });
  }

  // For real time location
  public realTimeLocation(lat, lng) {
    return Observable.create(observer => {
      this.socket.emit(
        'real_time_location',
        { longitude: lng, latitude: lat },
        data => {
          observer.next(data);
        }
      );
    });
  }

  // Track user
  public trackUser() {
    return Observable.create(observer => {
      this.socket.emit('track_user', {}, data => {
        observer.next(data);
      });
    });
  }
}

我在套接字连接后设置了jobStopjobStartjobStatusjobVerification侦听器。

我正在将此代码用于subscribe。这是我的示例代码。

this.chatService.jobStart$.subscribe(response => {
    console.log(response);
});

所有开始,停止,状态和验证均相同。

我已经检查了,但没有通过可观察到的方法来吸引听众。请查看我的代码并为我提供帮助。

非常感谢。

0 个答案:

没有答案