问题
I have the folowing chat service:
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
});
}
}
The code from the connect()
method generates the following error:
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
I used to have more errors (about bufferutil
, net
, utf-8-validate
) but those went away after running npm install bufferutil net utf-8-validate child_process
, only the one mentioning child_process
remained, although I've installed child_process
as well (not even sure what those packages do and why aren't they dependencies of twilio-chat, if they really are required).
The code is "inspired" from here: https://recursive.codes/blog/post/37 - it's the only example I could find that made any sense. I can't find a NativeScript example anywhere.
来源:https://stackoverflow.com/questions/56687363/using-twilio-chat-with-nativescript-and-angular