mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
fix: Re-added the option to use proxy agent forn non-mobile api (#148)
This commit is contained in:
committed by
GitHub
parent
aa9872b039
commit
54f8215eef
66
src/Socket/Client/mobile-socket-client.ts
Normal file
66
src/Socket/Client/mobile-socket-client.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { connect, Socket } from 'net'
|
||||
import { AbstractSocketClient } from './abstract-socket-client'
|
||||
|
||||
export class MobileSocketClient extends AbstractSocketClient {
|
||||
protected socket: Socket | null = null
|
||||
|
||||
get isOpen(): boolean {
|
||||
return this.socket?.readyState === 'open'
|
||||
}
|
||||
get isClosed(): boolean {
|
||||
return this.socket === null || this.socket?.readyState === 'closed'
|
||||
}
|
||||
get isClosing(): boolean {
|
||||
return this.socket === null || this.socket?.readyState === 'closed'
|
||||
}
|
||||
get isConnecting(): boolean {
|
||||
return this.socket?.readyState === 'opening'
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if(this.socket) {
|
||||
return
|
||||
}
|
||||
|
||||
if(this.config.agent) {
|
||||
|
||||
throw new Error('There are not support for proxy agent for mobile connection')
|
||||
} else {
|
||||
this.socket = connect({
|
||||
host: this.url.hostname,
|
||||
port: Number(this.url.port) || 443
|
||||
})
|
||||
}
|
||||
|
||||
this.socket.setMaxListeners(0)
|
||||
|
||||
const events = ['close', 'connect', 'data', 'drain', 'end', 'error', 'lookup', 'ready', 'timeout']
|
||||
|
||||
for(const event of events) {
|
||||
this.socket?.on(event, (...args: any[]) => this.emit(event, ...args))
|
||||
}
|
||||
|
||||
this.socket.on('data', (...args: any[]) => this.emit('message', ...args))
|
||||
this.socket.on('ready', (...args: any[]) => this.emit('open', ...args))
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if(!this.socket) {
|
||||
return
|
||||
}
|
||||
|
||||
return new Promise<void>(resolve => {
|
||||
this.socket!.end(resolve)
|
||||
this.socket = null
|
||||
})
|
||||
}
|
||||
|
||||
send(str: string | Uint8Array, cb?: (err?: Error) => void): boolean {
|
||||
if(this.socket === null) {
|
||||
return false
|
||||
}
|
||||
|
||||
return this.socket.write(str, undefined, cb)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user