官方文檔直譯
原文地址:http://socket.io/docs/client-api/#
IO(url:String, opts:Object):Socket
暴露在window
中作為全局的接口
如果被單獨(dú)創(chuàng)建(例如:/socket.io/socket.io.js
或者使用CDN)泽西,則結(jié)果應(yīng)該使用require('socket.io-client')
昙衅。
Exposed as the
io
global inwindow
if using the standalone build (eg:/socket.io/socket.io.js
or the CDN), or the result of callingrequire('socket.io-client')
.
當(dāng)被調(diào)用,它會(huì)根據(jù)提供的URL創(chuàng)建一個(gè)新的Manager
拖陆,并且將這個(gè)已經(jīng)存在的Manager
返回給之后調(diào)用這個(gè)方法的對象怔接, 除非multiplex
選項(xiàng)被設(shè)置為false
。設(shè)置這個(gè)選項(xiàng)等價(jià)于設(shè)置'force new connection': true
。
When called, it creates a new
Manager
for the given URL, and attempts to reuse an existingManager
for subsequent calls, unless themultiplex
option is passed withfalse
. Passing this option is the equivalent of passing'force new connection': true
.
其余的選項(xiàng)被傳遞給Manager
的構(gòu)造函數(shù)(詳細(xì)說明見下面)钞翔。
The rest of the options are passed to the
Manager
constructor (see below for details).
一個(gè)Socket
實(shí)例是在URL中通過路徑名返回指定的命名空間,默認(rèn)為/
席舍。例如布轿,如果一個(gè)url
是http://localhost/users
,一個(gè)傳輸連接將被與http://localhost
建立,并且 一個(gè) Socket.IO 連接將會(huì)被與/users
建立
A
Socket
instance is returned for the namespace specified by the pathname in the URL, defaulting to/
. For example, if theurl
ishttp://localhost/users
, a transport connection will be established tohttp://localhost
and a Socket.IO connection will be established to
/users
.
IO#協(xié)議
客戶端工作的Socket.io協(xié)議版本號(hào)汰扭。
IO#Socket
參考Socket
構(gòu)造函數(shù)稠肘。
IO#Manager
參考Manager
構(gòu)造函數(shù)。
IO#Emitter
參考Emitter
構(gòu)造函數(shù)萝毛。
Manager(url:String, opts:Object)
一個(gè)Manager
代表著一個(gè)和Socket.IO服務(wù)建立的鏈接项阴。一個(gè)或更多Socket
實(shí)例都是和這個(gè)manager又聯(lián)系的。對于 每個(gè)Socket實(shí)例都可以通過io
來被這個(gè)manager聯(lián)系笆包。
A
Manager
represents a connection to a given Socket.IO server. One or moreSocket
instances are associated with the manager. The manager can be accessed through theio
property of each Socket instance.
opts
通常在Socket
初始化時(shí)傳遞給底層的engine.io
环揽。
The
opts
are also passed toengine.io
upon initialization of the underlyingSocket
.
選項(xiàng):
-
reconnection
是否自動(dòng)重新連接(true
)。 -
reconnectionDelay
多長時(shí)間后建立一個(gè)新的連接(1000
)庵佣。 -
reconnectionDelayMax
重新連接的最大等待時(shí)間(5000
)歉胶。每一個(gè)嘗試重新連接都由reconnectionDelay
在指定的數(shù)量上增加。 -
timeout
當(dāng)一個(gè)connect_error
和connect_timeout
事件被發(fā)送之前的連接超時(shí)巴粪。
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 byreconnectionDelay
. -
timeout
connection timeout before aconnect_error
andconnect_timeout
events are emitted (20000
)
事件
-
connect
通今。有一個(gè)成功的連接則發(fā)送該事件。 -
connect_error
肛根。有一個(gè)鏈接錯(cuò)誤則發(fā)送該事件辫塌。
參數(shù):-
Object
錯(cuò)誤對象
-
-
connect_timeout
。有一個(gè)鏈接超時(shí)則發(fā)送該事件派哲。 -
reconnect
臼氨。有一個(gè)成功的重新連接則發(fā)送該事件。
參數(shù):-
Number
重新連接的次數(shù)
-
-
reconnect_attempt
芭届。開始一個(gè)重新連接則發(fā)送該事件储矩。 -
reconnecting
。正在重新連接則發(fā)送該事件喉脖。
參數(shù):-
Number
重新連接的次數(shù)
-
-
reconnect_error
椰苟。有一個(gè)重新連接的錯(cuò)誤則發(fā)送該事件。
參數(shù):-
Object
錯(cuò)誤對象
-
-
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 withinreconnectionAttempts
取決于Manager
,上面的事件也在獨(dú)立的sockets重新連接的時(shí)候被發(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
的選項(xiàng)洁仗,如果沒有傳參那么就返回它自己。
Manager#reconnectionAttempts(v:Boolean):Manager
設(shè)置reconnectionAttempts
選項(xiàng)性锭,如果沒有傳參那么就返回它自己赠潦。
Manager#reconnectionDelay(v:Boolean):Manager
設(shè)置reconectionDelay
選項(xiàng),如果沒有傳參那么就返回它自己草冈。
Manager#reconnectionDelayMax(v:Boolean):Manager
設(shè)置reconectionDelayMax
選項(xiàng)她奥,如果沒有傳參那么就返回它自己瓮增。
Manager#timeout(v:Boolean):Manager
設(shè)置timeout
選項(xiàng),如果沒有傳參那么就返回它自己哩俭。
Socket
事件
-
connect
绷跑。鏈接中則發(fā)送該事件。 -
error
凡资。有一個(gè)鏈接錯(cuò)誤則發(fā)送該事件
參數(shù):-
Object
錯(cuò)誤對象
-
-
disconnect
砸捏。斷開連接則發(fā)送該事件。 -
reconnect
隙赁。有一個(gè)成功的重新連接則發(fā)送該事件
參數(shù):-
Number
重新連接的次數(shù)
-
-
reconnect_attempt
垦藏。開始一個(gè)重新連接則發(fā)送該事件。 -
reconnecting
伞访。正在重新連接則發(fā)送該事件掂骏。
參數(shù):-
Number
重新連接的次數(shù)
-
-
reconnect_error
。有一個(gè)重新連接的錯(cuò)誤則發(fā)送該事件咐扭。
參數(shù):-
Object
錯(cuò)誤對象
-
-
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:-
Object
error object
-
-
reconnect_failed
. Fired when couldn’t reconnect withinreconnectionAttempts