You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.ts 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Socket } from "socket.io"
  2. import * as U from '../Utils'
  3. import * as I from '../Interfaces'
  4. import * as socketio from 'socket.io-client'
  5. import { ClientConfig } from "../Types"
  6. export const defaultClientConfig: ClientConfig = {
  7. protocol: 'http',
  8. reconnectionAttempts: 2,
  9. reconnectionDelay: 200,
  10. timeout: 450,
  11. reconnection: false,
  12. }
  13. export class PromiseIOClient {
  14. static connect = (port: number, host = "localhost", options : ClientConfig = defaultClientConfig): Promise<I.Socket> => new Promise((res, rej) => {
  15. try {
  16. if(options.path && !options.path.startsWith('/')){
  17. options.path = "/"+options.path
  18. }
  19. const address = `${host}:${port}`
  20. const socket = socketio(`${options.protocol?options.protocol:'http'}://${address}`, options)
  21. socket.on('connect_error', e => {
  22. sock.emit('error', e)
  23. rej(e)
  24. })
  25. socket['address'] = address
  26. const sock = U.makePioSocket(socket)
  27. socket.on('connect', ()=>{
  28. res(sock)
  29. })
  30. /*
  31. socket.on('connect_timeout', ()=>console.log('connect_timeout'))
  32. socket.on('disconnect', ()=>console.log('disconnect'))
  33. socket.on('reconnect', ()=>console.log('reconnect'))
  34. socket.on('reconnect_attempt', ()=>console.log('reconnect_attempt'))
  35. socket.on('reconnecting', ()=>console.log('reconnecting'));
  36. socket.on('reconnect_failed', ()=>console.log('reconnect_failed'));
  37. socket.on('reconnecting', ()=>console.log('reconnecting'));
  38. */
  39. } catch (e) {
  40. rej(e)
  41. }
  42. })
  43. }