Socket.io客戶端API

官方文檔直譯


原文地址:http://socket.io/docs/client-api/#


IO(url:String, opts:Object):Socket

暴露在window中作為全局的接口如果被單獨創(chuàng)建(例如:/socket.io/socket.io.js或者使用CDN),則結(jié)果應(yīng)該使用require('socket.io-client')。

Exposed as the io global in window if using the standalone build (eg: /socket.io/socket.io.js or the CDN), or the result of calling require('socket.io-client').

當被調(diào)用,它會根據(jù)提供的URL創(chuàng)建一個新的Manager,并且將這個已經(jīng)存在的Manager返回給之后調(diào)用這個方法的對象, 除非multiplex選項被設(shè)置為false。設(shè)置這個選項等價于設(shè)置'force new connection': true。

When called, it creates a new Manager for the given URL, and attempts to reuse an existing Manager for subsequent calls, unless the multiplex option is passed with false. Passing this option is the equivalent of passing 'force new connection': true.

其余的選項被傳遞給Manager的構(gòu)造函數(shù)(詳細說明見下面)。

The rest of the options are passed to the Manager constructor (see below for details).

一個Socket實例是在URL中通過路徑名返回指定的命名空間,默認為/。例如,如果一個urlhttp://localhost/users,一個傳輸連接將被與http://localhost建立,并且 一個 Socket.IO 連接將會被與/users建立

A Socket instance is returned for the namespace specified by the pathname in the URL, defaulting to /. For example, if the url is http://localhost/users, a transport connection will be established to http://localhost and a Socket.IO connection will be established to
/users.

IO#協(xié)議

客戶端工作的Socket.io協(xié)議版本號。

IO#Socket

參考Socket構(gòu)造函數(shù)。

IO#Manager

參考Manager構(gòu)造函數(shù)。

IO#Emitter

參考Emitter構(gòu)造函數(shù)。

Manager(url:String, opts:Object)

一個Manager代表著一個和Socket.IO服務(wù)建立的鏈接。一個或更多Socket實例都是和這個manager又聯(lián)系的。對于 每個Socket實例都可以通過io來被這個manager聯(lián)系。

A Manager represents a connection to a given Socket.IO server. One or more Socket instances are associated with the manager. The manager can be accessed through the io property of each Socket instance.

opts通常在Socket初始化時傳遞給底層的engine.io

The opts are also passed to engine.io upon initialization of the underlying Socket.

選項:

  • reconnection是否自動重新連接(true)。
  • reconnectionDelay多長時間后建立一個新的連接(1000)。
  • reconnectionDelayMax重新連接的最大等待時間(5000)。每一個嘗試重新連接都由reconnectionDelay在指定的數(shù)量上增加。
  • timeout當一個connect_errorconnect_timeout事件被發(fā)送之前的連接超時。
  • reconnection whether to reconnect automatically (true)
  • reconnectionDelay how long to wait before attempting a new reconnection (1000)
  • reconnectionDelayMax maximum amount of time to wait between reconnections (5000). Each attempt increases the reconnection by the amount specified by reconnectionDelay.
  • timeout connection timeout before a connect_error and connect_timeout events are emitted (20000)

事件

  • connect。有一個成功的連接則發(fā)送該事件。
  • connect_error。有一個鏈接錯誤則發(fā)送該事件。
    參數(shù):
    • Object 錯誤對象
  • connect_timeout。有一個鏈接超時則發(fā)送該事件。
  • reconnect。有一個成功的重新連接則發(fā)送該事件。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_attempt。開始一個重新連接則發(fā)送該事件。
  • reconnecting。正在重新連接則發(fā)送該事件。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_error。有一個重新連接的錯誤則發(fā)送該事件。
    參數(shù):
    • Object 錯誤對象
  • reconnect_failed。在reconnectionAttempts內(nèi)無法重新連接則發(fā)送該事件。
  • connect. Fired upon a successful connection.
  • connect_error. Fired upon a connection error.
    Parameters:
    • Object error object
  • connect_timeout. Fired upon a connection timeout.
  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number
  • reconnect_attempt. Fired upon an attempt to reconnect.
  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number
  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Object error object
  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts

取決于Manager,上面的事件也在獨立的sockets重新連接的時候被發(fā)送。

The events above are also emitted on the individual sockets that reconnect that depend on this Manager.

Manager#reconnection(v:Boolean):Manager

設(shè)置reconnection的選項,如果沒有傳參那么就返回它自己。

Manager#reconnectionAttempts(v:Boolean):Manager

設(shè)置reconnectionAttempts選項,如果沒有傳參那么就返回它自己。

Manager#reconnectionDelay(v:Boolean):Manager

設(shè)置reconectionDelay選項,如果沒有傳參那么就返回它自己。

Manager#reconnectionDelayMax(v:Boolean):Manager

設(shè)置reconectionDelayMax選項,如果沒有傳參那么就返回它自己。

Manager#timeout(v:Boolean):Manager

設(shè)置timeout選項,如果沒有傳參那么就返回它自己。

Socket

事件

  • connect。鏈接中則發(fā)送該事件。
  • error。有一個鏈接錯誤則發(fā)送該事件
    參數(shù):
    • Object 錯誤對象
  • disconnect。斷開連接則發(fā)送該事件。
  • reconnect。有一個成功的重新連接則發(fā)送該事件
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_attempt。開始一個重新連接則發(fā)送該事件。
  • reconnecting。正在重新連接則發(fā)送該事件。
    參數(shù):
    • Number重新連接的次數(shù)
  • reconnect_error。有一個重新連接的錯誤則發(fā)送該事件。
    參數(shù):
    • Object 錯誤對象
  • reconnect_failed。在reconnectionAttempts內(nèi)無法重新連接則發(fā)送該事件。
  • connect. Fired upon connecting.
  • error. Fired upon a connection error
    Parameters:
    • Object error data
  • disconnect. Fired upon a disconnection.
  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number
  • reconnect_attempt. Fired upon an attempt to reconnect.
  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number
  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Objecterror object
  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容