ObjectWebSocket
WebSocket is a full-duplex communication protocol based on the TCP protocol. It establishes an uninterrupted connection between the browser and the server, can realize real-time two-way data transmission, and can support data transmission in any format. In fibjs, the WebSocket support module provides the corresponding API interface, which can realize the development of WebSocket server and client.
The WebSocket support module is just an implementation of the WebSocket protocol and needs to work on top of the HTTP protocol. On the server side, the HTTP request can be converted into a WebSocket connection through the upgrade function, while on the client side, the server address to be connected needs to be specified through the URL of the WebSocket protocol.
Example of starting a WebSocket server:
1
2
3
4
5
6
7
8
9
10
11var ws = require('ws');
var http = require('http');
var svr = new http.Server(80, {
'/ws': ws.upgrade(conn => {
conn.onmessage = e => {
conn.send('fibjs:' + e.data);
};
})
});
svr.start();
Example of establishing a connection to the above server in the client:
1
2
3
4
5
6
7
8
9
10
11
12var ws = require("ws");
var conn = new ws.Socket("ws://127.0.0.1/ws");
// emit open event
conn.onopen = () => {
console.log("websocket connected");
conn.send("hi");
};
// emit close event
conn.onmessage = evt => {
console.log("websocket receive: " + evt.data);
};
inheritance relationship
Constructor
WebSocket
WebSocket constructor
1
2
3new WebSocket(String url,
String protocol = "",
String origin = "");
Call parameters:
- url: String, specifies the connected server
- protocol: String, specifies the handshake protocol, the default is ""
- origin: String, specifies the source of simulation during handshake, the default is ""
WebSocket constructor
1
2new WebSocket(String url,
Object opts);
Call parameters:
- url: String, specifies the connected server
- opts: Object, connection option, default is {}
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8{
"protocol": "", // specify the sub-protocol, default is ""
"origin": "", // specify the origin, default is ""
"perMessageDeflate": false, // specify whether to enable permessage-deflate, default is false
"maxPayload": 67108864, // specify the max payload size, default is 64MB
"httpClient": hc, // specify the http client, default is null, use the global http client
"headers": // specify the http headers, default is {}
}
static properties
defaultMaxListeners
Integer, the default global maximum number of listeners
1static Integer WebSocket.defaultMaxListeners;
member properties
url
String, query the server the current object is connected to
1readonly String WebSocket.url;
protocol
String, query the protocol of current object connection
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 events, 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, queries and binds connection closing events, equivalent to on("close", func);
1Function WebSocket.onclose;
onerror
Function, query and bind events when errors occur, 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, specifies the shutdown code, the allowed value is 3000-4999 or 1000, the default is 1000
- reason: String, specifies the reason for shutdown, the default is ""
send
Send a text to the other party
1WebSocket.send(String data);
Call parameters:
- data: String, specifies the text to be sent
Send a piece of binary data to the other party
1WebSocket.send(Buffer data);
Call parameters:
- data:Buffer, specify the binary data sent
ref
Keep the fibjs process from exiting and prevent the fibjs process from exiting during object binding
1WebSocket WebSocket.ref();
Return results:
- WebSocket, returns the current object
unref
Allow the fibjs process to exit. Allow the fibjs process to exit during object binding.
1WebSocket WebSocket.unref();
Return results:
- WebSocket, returns the current object
on
Bind an event handler to the object
1
2Object WebSocket.on(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object
1Object WebSocket.on(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
addListener
Bind an event handler to the object
1
2Object WebSocket.addListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object
1Object WebSocket.addListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
prependListener
Bind an event handler to the object's origin
1
2Object WebSocket.prependListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object's origin
1Object WebSocket.prependListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
once
Bind a one-time event handler to the object. The one-time handler will only be triggered once.
1
2Object WebSocket.once(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind a one-time event handler to the object. The one-time handler will only be triggered once.
1Object WebSocket.once(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
prependOnceListener
Bind an event handler to the object's origin
1
2Object WebSocket.prependOnceListener(String ev,
Function func);
Call parameters:
- ev: String, specifies the name of the event
- func: Function, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Bind an event handler to the object's origin
1Object WebSocket.prependOnceListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
off
Unassign a 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, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Cancel all functions in the object processing queue
1Object WebSocket.off(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
Unassign a function from the object processing queue
1Object WebSocket.off(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
removeListener
Unassign a 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, specify the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
Cancel all functions in the object processing queue
1Object WebSocket.removeListener(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
Unassign a function from the object processing queue
1Object WebSocket.removeListener(Object map);
Call parameters:
- map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function
Return results:
- Object, returns the event object itself to facilitate chain calls
removeAllListeners
Cancels all listeners for all events from the object's processing queue. 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 results:
- Object, returns the event object itself to facilitate chain calls
Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.
1Object WebSocket.removeAllListeners(Array evs = []);
Call parameters:
- evs: Array, specify the name of the event
Return results:
- Object, returns the event object itself to facilitate chain calls
setMaxListeners
The default limit on the number of listeners, for compatibility only
1WebSocket.setMaxListeners(Integer n);
Call parameters:
- n: Integer, specify the number of events
getMaxListeners
Gets the default limit number of listeners, for compatibility only
1Integer WebSocket.getMaxListeners();
Return results:
- Integer, returns the default limit quantity
listeners
Query the listener array for the specified event of the object
1Array WebSocket.listeners(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Array, returns the listener array for the specified event
listenerCount
Query the number of listeners for the specified event of the object
1Integer WebSocket.listenerCount(String ev);
Call parameters:
- ev: String, specifies the name of the event
Return results:
- Integer, returns the number of listeners for the specified event
Query the number of listeners for the specified event of the 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 results:
- Integer, returns the number of listeners for the specified event
eventNames
Query listener event name
1Array WebSocket.eventNames();
Return results:
- 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 will be passed to the event processing function
Return results:
- 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, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String WebSocket.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value WebSocket.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable