Использование twilio-чата с NativeScript и Angular - PullRequest
0 голосов
/ 20 июня 2019

У меня есть следующая служба чата:

import { Injectable, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import * as Twilio from 'twilio-chat';
import Client from 'twilio-chat';
import { Channel } from 'twilio-chat/lib/channel';

@Injectable({
  providedIn: 'root'
})
export class ChatService {
  chatClient: Client;
  currentChannel: Channel;
  chatConnectedEmitter: EventEmitter<any> = new EventEmitter<any>();
  chatDisconnectedEmitter: EventEmitter<any> = new EventEmitter<any>();

  private apiUrl = 'https://my-api.com/api';

  constructor(private http: HttpClient) {}

  getToken(): Observable<{ token: string }> {
    return this.http.get<{ token: string }>(`${this.apiUrl}/chat/token/`);
  }

  connect(token: string) {
    Twilio.Client.create(token)
      .then((client: Client) => {
        this.chatClient = client;
        this.chatConnectedEmitter.emit(true);
      })
      .catch((err: any) => {
        this.chatDisconnectedEmitter.emit(true);
        if (err.message.indexOf('token is expired')) {
          // do something when token expires
        }
      });
  }

  getPublicChannels() {
    return this.chatClient.getPublicChannelDescriptors();
  }

  getChannel(sid: string): Promise<Channel> {
    return this.chatClient.getChannelBySid(sid);
  }

  createChannel(friendlyName: string, isPrivate: boolean = false) {
    return this.chatClient.createChannel({
      friendlyName,
      isPrivate,
      uniqueName: 'channel_1' // << generate unique channel name here
    });
  }
}

Код метода connect() генерирует следующую ошибку:

ERROR in ../node_modules/xmlhttprequest/lib/XMLHttpRequest.js
Module not found: Error: Can't resolve 'child_process' in 'D:\www\chat\node_modules\xmlhttprequest\lib'
 @ ../node_modules/xmlhttprequest/lib/XMLHttpRequest.js 15:12-36
 @ ../node_modules/twilio-transport/lib/transport.js
 @ ../node_modules/twilio-mcs-client/lib/client.js
 @ ../node_modules/twilio-mcs-client/lib/index.js
 @ ../node_modules/twilio-chat/lib/client.js
 @ ../node_modules/twilio-chat/lib/index.js
 @ ./app/shared/chat.service.ts
 @ ./app/chat/_state/chat.state.ts
 @ ./app/chat/chat.module.ts
 @ ./app/app.module.ts
 @ ./main.ts

Я имел обыкновение иметь больше ошибок (о bufferutil, net, utf-8-validate), но они пропали после запуска npm install bufferutil net utf-8-validate child_process, остался только тот, который упоминал child_process, хотя я также установил child_process (даже не уверен, что эти пакеты делают и почемуне являются ли они зависимостями twilio-chat, если они действительно необходимы).

Код «вдохновлен» отсюда: https://recursive.codes/blog/post/37 - это единственный пример, который я мог найти, который имел какой-то смысл.Я нигде не могу найти пример NativeScript.

...