import { Socket } from "socket.io" import * as U from '../Utils' import * as I from '../Interfaces' import * as socketio from 'socket.io-client' import { ClientConfig } from "../Types" export const defaultClientConfig: ClientConfig = { protocol: 'http', reconnectionAttempts: 2, reconnectionDelay: 200, timeout: 450, reconnection: false, } export class PromiseIOClient { static connect = (port: number, host = "localhost", options : ClientConfig = defaultClientConfig): Promise => new Promise((res, rej) => { try { if(options.path && !options.path.startsWith('/')){ options.path = "/"+options.path } const address = `${host}:${port}` const socket = socketio(`${options.protocol?options.protocol:'http'}://${address}`, options) socket.on('connect_error', e => { sock.emit('error', e) rej(e) }) socket['address'] = address const sock = U.makePioSocket(socket) socket.on('connect', ()=>{ res(sock) }) /* socket.on('connect_timeout', ()=>console.log('connect_timeout')) socket.on('disconnect', ()=>console.log('disconnect')) socket.on('reconnect', ()=>console.log('reconnect')) socket.on('reconnect_attempt', ()=>console.log('reconnect_attempt')) socket.on('reconnecting', ()=>console.log('reconnecting')); socket.on('reconnect_failed', ()=>console.log('reconnect_failed')); socket.on('reconnecting', ()=>console.log('reconnecting')); */ } catch (e) { rej(e) } }) }