Object WebSocket
WebSocket packet protocol conversion handler
Used to convert Http protocol to WebSocket packet protocol message. How to create:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21var ws = require('ws');
var http = require('http');
var serv = new http.Server(8811, ws.upgrade((conn) => {
conn.onmessage = msg => {
conn.send(new Date());
};
}));
serv.run(r => 0);
var sock = new ws.Socket('ws://127.0.0.1:8811');
sock.on('open', () => {
setInterval(() => {
sock.send('get date');
}, 1000);
});
sock.onmessage = evt => {
console.log(evt.data);
}
inheritance relationship
Constructor
WebSocket
WebSocket constructor
1
2
3new WebSocket(String url,
String protocol = "",
String origin = "");
Call parameters:
- url: String, specifies the server to connect to
- protocol: String, specifies the handshake protocol, the default is ""
- origin: String, specifies the source to simulate during handshake, default is ""
WebSocket constructor
1
2new WebSocket(String url,
Object opts);
Call parameters:
- url: String, specifies the server to connect to
- opts: Object, connection options, default is {}
opts contains additional options for the request, the supported ones are as follows:
1
2
3
4
5
6
7
8{
"protocol": "", // 指定握手协议,缺省为空
"origin": "", // 指定握手时模拟的源,缺省为空
"perMessageDeflate": false, // 指定是否支持压缩,缺省不支持
"maxPayload": 67108864, // 指定最大数据包尺寸,缺省为 67108864
"httpClient": hc, // 自定义 httpClient 对象,缺省使用全局 httpClient
"headers": // 指定 http(s) 连接时携带的 header,缺省为 {},
}
static properties
defaultMaxListeners
Integer, the default global maximum number of listeners
1static Integer WebSocket.defaultMaxListeners;
Member properties
url
String, query the server connected to the current object
1readonly String WebSocket.url;
protocol
String, query the protocol when the current object is connected
1readonly String WebSocket.protocol;
origin
String, query the source of the current object connection
1readonly String WebSocket.origin;
readyState
Integer, query the connection status of the current object, seews
1readonly Integer WebSocket.readyState;
onopen
Function, query and bind connection success event, equivalent to on("open", func);
1Function WebSocket.onopen;
onmessage
Function, query and bind events that receive messages from the other party, equivalent to on("message", func);
1Function WebSocket.onmessage;
onclose
Function, query and bind the event that the connection is closed, equivalent to on("close", func);
1Function WebSocket.onclose;
onerror
Function, query and bind the event of the error, equivalent to on("error", func);
1Function WebSocket.onerror;
member function
close
Close the current connection, this operation will send a CLOSE packet to the other party and wait for the other party to respond
1
2WebSocket.close(Integer code = 1000,
String reason = "");
Call parameters:
- code: Integer, specify the code to close, the allowed value is 3000-4999 or 1000, the default is 1000
- reason: String, specify the reason for closing, default is ""
send
send a text to the other party
1WebSocket.send(String data);
Call parameters:
- data: String, specifies the text to send
Send a piece of binary data to the other party
1WebSocket.send(Buffer data);
Call parameters:
- data:Buffer, specify the binary data to send
ref
Keep the fibjs process from exiting and prevent the fibjs process from exiting during object binding
1WebSocket WebSocket.ref();
Return result:
- WebSocket, returns the current object
unref
Allow fibjs process to exit, allow fibjs process to exit during object binding
1WebSocket WebSocket.unref();
Return result:
- WebSocket, returns the current object
on
Bind an event handler to an object
1
2Object WebSocket.on(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Bind an event handler to an object
1Object WebSocket.on(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
addListener
Bind an event handler to an object
1
2Object WebSocket.addListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Bind an event handler to an object
1Object WebSocket.addListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
prependListener
Bind an event handler to the object start
1
2Object WebSocket.prependListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Bind an event handler to the object start
1Object WebSocket.prependListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
once
Bind a one-time event handler to the object, the one-time handler will only fire once
1
2Object WebSocket.once(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Bind a one-time event handler to the object, the one-time handler will only fire once
1Object WebSocket.once(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
prependOnceListener
Bind an event handler to the object start
1
2Object WebSocket.prependOnceListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Bind an event handler to the object start
1Object WebSocket.prependOnceListener(Object map);
Call parameters:
- map: Object, specify the event mapping relationship, the object property name will be used as the event name, and the property value will be used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
off
cancel the specified function from the object processing queue
1
2Object WebSocket.off(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Cancel all functions in the object processing queue
1Object WebSocket.off(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return result:
- Object, returns the event object itself for easy chaining
cancel the specified function from the object processing queue
1Object WebSocket.off(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object property name is used as the event name, and the property value is used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
removeListener
cancel the specified function from the object processing queue
1
2Object WebSocket.removeListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specifies the event handler function
Return result:
- Object, returns the event object itself for easy chaining
Cancel all functions in the object processing queue
1Object WebSocket.removeListener(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return result:
- Object, returns the event object itself for easy chaining
cancel the specified function from the object processing queue
1Object WebSocket.removeListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object property name is used as the event name, and the property value is used as the event handler function
Return result:
- Object, returns the event object itself for easy chaining
removeAllListeners
Cancels all listeners for all events from the object's processing queue, or if an event is specified, removes all listeners for the specified event.
1Object WebSocket.removeAllListeners(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return result:
- Object, returns the event object itself for easy chaining
Cancels all listeners for all events from the object's processing queue, or if an event is specified, removes all listeners for the specified event.
1Object WebSocket.removeAllListeners(Array evs = []);
Call parameters:
- evs: Array, specifying the name of the event
Return result:
- Object, returns the event object itself for easy chaining
setMaxListeners
The default limit on the number of listeners, for compatibility only
1WebSocket.setMaxListeners(Integer n);
Call parameters:
- n: Integer, specifies the number of events
getMaxListeners
Gets the number of default limits for listeners, for compatibility only
1Integer WebSocket.getMaxListeners();
Return result:
- Integer, returns the default limit number
listeners
Query the array of listeners for the specified event of the object
1Array WebSocket.listeners(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return result:
- Array, returns an array of listeners for the specified event
listenerCount
The number of listeners for the specified event of the query object
1Integer WebSocket.listenerCount(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return result:
- Integer, returns the number of listeners for the specified event
The number of listeners for the specified event of the query object
1
2Integer WebSocket.listenerCount(Value o,
String ev);
Call parameters:
- o: Value, specifies the object of the query
- ev: String, specifies the name of the event
Return result:
- Integer, returns the number of listeners for the specified event
eventNames
Query listener event name
1Array WebSocket.eventNames();
Return result:
- Array, returns an array of event names
emit
Actively trigger an event
1
2Boolean WebSocket.emit(String ev,
...args);
Call parameters:
- ev: String, event name
- args: ..., event parameters, which will be passed to the event handler
Return result:
- Boolean, returns the event trigger status, returns true if there is a response event, otherwise returns false
toString
Returns the string representation of the object, generally returns "[Native Object]", the object can be reimplemented according to its own characteristics
1String WebSocket.toString();
Return result:
- String, returns the string representation of the object
toJSON
Returns the JSON format representation of the object, generally returns the set of readable properties defined by the object
1Value WebSocket.toJSON(String key = "");
Call parameters:
- key: String, unused
Return result:
- Value, returns a value containing JSON serializable