* Client object * @param {ClientOptions} clientOptions TradingView client options
(clientOptions = {})
| 225 | * @param {ClientOptions} clientOptions TradingView client options |
| 226 | */ |
| 227 | constructor(clientOptions = {}) { |
| 228 | if (clientOptions.DEBUG) global.TW_DEBUG = clientOptions.DEBUG; |
| 229 | |
| 230 | const server = clientOptions.server || 'data'; |
| 231 | this.#ws = new WebSocket(`wss://${server}.tradingview.com/socket.io/websocket?type=chart`, { |
| 232 | origin: 'https://www.tradingview.com', |
| 233 | }); |
| 234 | |
| 235 | if (clientOptions.token) { |
| 236 | misc.getUser( |
| 237 | clientOptions.token, |
| 238 | clientOptions.signature ? clientOptions.signature : '', |
| 239 | clientOptions.location ? clientOptions.location : 'https://tradingview.com', |
| 240 | ).then((user) => { |
| 241 | this.#sendQueue.unshift(protocol.formatWSPacket({ |
| 242 | m: 'set_auth_token', |
| 243 | p: [user.authToken], |
| 244 | })); |
| 245 | this.#logged = true; |
| 246 | this.sendQueue(); |
| 247 | }).catch((err) => { |
| 248 | this.#handleError('Credentials error:', err.message); |
| 249 | }); |
| 250 | } else { |
| 251 | this.#sendQueue.unshift(protocol.formatWSPacket({ |
| 252 | m: 'set_auth_token', |
| 253 | p: ['unauthorized_user_token'], |
| 254 | })); |
| 255 | this.#logged = true; |
| 256 | this.sendQueue(); |
| 257 | } |
| 258 | |
| 259 | this.#ws.on('open', () => { |
| 260 | this.#handleEvent('connected'); |
| 261 | this.sendQueue(); |
| 262 | }); |
| 263 | |
| 264 | this.#ws.on('close', () => { |
| 265 | this.#logged = false; |
| 266 | this.#handleEvent('disconnected'); |
| 267 | }); |
| 268 | |
| 269 | this.#ws.on('error', (err) => { |
| 270 | this.#handleError('WebSocket error:', err.message); |
| 271 | }); |
| 272 | |
| 273 | this.#ws.on('message', (data) => this.#parsePacket(data)); |
| 274 | } |
| 275 | |
| 276 | /** @type {ClientBridge} */ |
| 277 | #clientBridge = { |
nothing calls this directly
no test coverage detected