MCPcopy
hub / github.com/wechaty/wechaty / initWebSocket

Method initWebSocket

src/io.ts:217–250  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

215 }
216
217 private async initWebSocket (): Promise<WebSocket> {
218 log.verbose('Io', 'initWebSocket()')
219 // this.state.current('on', false)
220
221 // const auth = 'Basic ' + new Buffer(this.setting.token + ':X').toString('base64')
222 const auth = 'Token ' + this.options.token
223 const headers = { Authorization: auth }
224
225 if (!this.options.apihost) {
226 throw new Error('no apihost')
227 }
228 let endpoint = 'wss://' + this.options.apihost + '/v0/websocket'
229
230 // XXX quick and dirty: use no ssl for API_HOST other than official
231 // FIXME: use a configurable VARIABLE for the domain name at here:
232 if (!/api\.chatie\.io/.test(this.options.apihost)) {
233 endpoint = 'ws://' + this.options.apihost + '/v0/websocket'
234 }
235
236 const ws = this.ws = new WebSocket(endpoint, this.protocol, { headers })
237
238 ws.on('open', () => this.wsOnOpen(ws))
239 ws.on('message', data => this.wsOnMessage(data))
240 ws.on('error', e => this.wsOnError(e))
241 ws.on('close', (code, reason) => this.wsOnClose(ws, code, reason))
242
243 await new Promise((resolve, reject) => {
244 ws.once('open', resolve)
245 ws.once('error', reject)
246 ws.once('close', reject)
247 })
248
249 return ws
250 }
251
252 private async wsOnOpen (ws: WebSocket): Promise<void> {
253 if (this.protocol !== ws.protocol) {

Callers 2

startMethod · 0.95
reconnectMethod · 0.95

Calls 5

wsOnOpenMethod · 0.95
wsOnMessageMethod · 0.95
wsOnErrorMethod · 0.95
wsOnCloseMethod · 0.95
onMethod · 0.80

Tested by

no test coverage detected