官方文檔直譯
原文地址: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
ioglobal inwindowif using the standalone build (eg:/socket.io/socket.io.jsor the CDN), or the result of callingrequire('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
Managerfor the given URL, and attempts to reuse an existingManagerfor subsequent calls, unless themultiplexoption is passed withfalse. 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
Managerconstructor (see below for details).
一個Socket實例是在URL中通過路徑名返回指定的命名空間,默認為/。例如,如果一個url是http://localhost/users,一個傳輸連接將被與http://localhost建立,并且 一個 Socket.IO 連接將會被與/users建立
A
Socketinstance is returned for the namespace specified by the pathname in the URL, defaulting to/. For example, if theurlishttp://localhost/users, a transport connection will be established tohttp://localhostand 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
Managerrepresents a connection to a given Socket.IO server. One or moreSocketinstances are associated with the manager. The manager can be accessed through theioproperty of each Socket instance.
opts通常在Socket初始化時傳遞給底層的engine.io。
The
optsare also passed toengine.ioupon initialization of the underlyingSocket.
選項:
-
reconnection是否自動重新連接(true)。 -
reconnectionDelay多長時間后建立一個新的連接(1000)。 -
reconnectionDelayMax重新連接的最大等待時間(5000)。每一個嘗試重新連接都由reconnectionDelay在指定的數(shù)量上增加。 -
timeout當一個connect_error和connect_timeout事件被發(fā)送之前的連接超時。
reconnectionwhether to reconnect automatically (true)
-
reconnectionDelayhow long to wait before attempting a new reconnection (1000) -
reconnectionDelayMaxmaximum amount of time to wait between reconnections (5000). Each attempt increases the reconnection by the amount specified byreconnectionDelay. -
timeoutconnection timeout before aconnect_errorandconnect_timeoutevents 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:-
Objecterror object
-
-
connect_timeout. Fired upon a connection timeout. -
reconnect. Fired upon a successful reconnection.
Parameters:-
Numberreconnection attempt number
-
-
reconnect_attempt. Fired upon an attempt to reconnect. -
reconnecting. Fired upon an attempt to reconnect.
Parameters:-
Numberreconnection attempt number
-
-
reconnect_error. Fired upon a reconnection attempt error.
Parameters:-
Objecterror object
-
-
reconnect_failed. Fired when couldn’t reconnect withinreconnectionAttempts
取決于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:-
Objecterror data
-
-
disconnect. Fired upon a disconnection. -
reconnect. Fired upon a successful reconnection.
Parameters:-
Numberreconnection attempt number
-
-
reconnect_attempt. Fired upon an attempt to reconnect. -
reconnecting. Fired upon an attempt to reconnect.
Parameters:-
Numberreconnection attempt number
-
-
reconnect_error. Fired upon a reconnection attempt error.
Parameters:-
Objecterror object
-
-
reconnect_failed. Fired when couldn’t reconnect withinreconnectionAttempts