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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. callTimeoutMs: 0,
  13. }
  14. export class PromiseIOClient {
  15. static connect = (port: number, host = "localhost", options : ClientConfig = defaultClientConfig): Promise<I.Socket> => new Promise((res, rej) => {
  16. try {
  17. if(options.path && !options.path.startsWith('/')){
  18. options.path = "/"+options.path
  19. }
  20. const address = `${host}:${port}`
  21. const socket = socketio(`${options.protocol?options.protocol:'http'}://${address}`, options)
  22. socket.on('connect_error', e => {
  23. sock.emit('error', e)
  24. rej(e)
  25. })
  26. socket['address'] = address
  27. const sock = U.makePioSocket(socket)
  28. socket.on('connect', ()=>{
  29. res(sock)
  30. })
  31. /*
  32. socket.on('connect_timeout', ()=>console.log('connect_timeout'))
  33. socket.on('disconnect', ()=>console.log('disconnect'))
  34. socket.on('reconnect', ()=>console.log('reconnect'))
  35. socket.on('reconnect_attempt', ()=>console.log('reconnect_attempt'))
  36. socket.on('reconnecting', ()=>console.log('reconnecting'));
  37. socket.on('reconnect_failed', ()=>console.log('reconnect_failed'));
  38. socket.on('reconnecting', ()=>console.log('reconnecting'));
  39. */
  40. } catch (e) {
  41. rej(e)
  42. }
  43. })
  44. }