我有以下聊天服务:
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示例。