refs #125 Add proxy support to misskey websocket

This commit is contained in:
AkiraFukushima 2020-03-13 00:50:37 +09:00
parent fa7c8d0ee1
commit 2b5d22c01d
2 changed files with 21 additions and 6 deletions

View file

@ -468,13 +468,13 @@ namespace MisskeyAPI {
*/
public socket(
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
listId?: string | null
listId: string | null = null
): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = this.baseUrl + '/streaming'
const streaming = new WebSocket(url, channel, this.accessToken, listId)
const streaming = new WebSocket(url, channel, this.accessToken, listId, this.userAgent, this.proxyConfig)
process.nextTick(() => {
streaming.start()
})

View file

@ -3,6 +3,7 @@ import moment, { Moment } from 'moment'
import { v4 as uuid } from 'uuid'
import { EventEmitter } from 'events'
import { WebSocketInterface } from '../megalodon'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import MisskeyAPI from './api_client'
/**
@ -14,6 +15,8 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
public url: string
public channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list'
public parser: Parser
public headers: { [key: string]: string }
public proxyConfig: ProxyConfig | false = false
public listId: string | null = null
private _accessToken: string
private _reconnectInterval: number
@ -36,15 +39,19 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
url: string,
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
accessToken: string,
listId?: string | null
listId: string | null,
userAgent: string,
proxyConfig: ProxyConfig | false = false
) {
super()
this.url = url
this.parser = new Parser()
this.channel = channel
if (listId) {
this.listId = listId
this.headers = {
'User-Agent': userAgent
}
this.listId = listId
this.proxyConfig = proxyConfig
this._accessToken = accessToken
this._reconnectInterval = 10000
this._reconnectMaxAttempts = Infinity
@ -108,7 +115,15 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
* Connect to the endpoint.
*/
private _connect(): WS {
const cli: WS = new WS(`${this.url}?i=${this._accessToken}`)
let options: WS.ClientOptions = {
headers: this.headers
}
if (this.proxyConfig) {
options = Object.assign(options, {
agent: proxyAgent(this.proxyConfig)
})
}
const cli: WS = new WS(`${this.url}?i=${this._accessToken}`, options)
return cli
}