12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- 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,
- callTimeoutMs: 0,
- }
-
- export class PromiseIOClient {
-
- static connect = (port: number, host = "localhost", options : ClientConfig = defaultClientConfig): Promise<I.Socket> => new Promise((res, rej) => {
- try {
- if(options.path && !options.path.startsWith('/')){
- options.path = "/"+options.path
- }
-
- const address = `${host}:${port}`
- const socket = socketio.io(`${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)
- }
- })
- }
|