Object built-in object

Object WebSocket

WebSocket packet protocol conversion processor

It is used to convert Http protocol into WebSocket packet protocol message. Creation method:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
var 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

Constructor

WebSocket

WebSocket constructor

1 2 3
new WebSocket(String url, String protocol = "", String origin = "");

Call parameters:

  • url : String, specify the connected server
  • protocol : String, specify the handshake protocol, the default is ""
  • origin : String, specifies the origin simulated during handshake, the default is ""

WebSocket constructor

1 2
new WebSocket(String url, Object opts);

Call parameters:

  • url : String, specify the connected server
  • opts : Object, connection options, the default is {}

opts contains additional options requested, and the supported content is as follows:

1 2 3 4 5 6 7 8
{ "protocol": "", // 指定握手协议,缺省为空 "origin": "", // 指定握手时模拟的源,缺省为空 "perMessageDeflate": true, // 指定是否支持压缩,缺省支持 "maxPayload": 67108864, // 指定最大数据包尺寸,缺省为 67108864 "httpClient": hc, // 自定义 httpClient 对象,缺省使用全局 httpClient "headers": // 指定 http(s) 连接时携带的 header,缺省为 {}, }

Static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer WebSocket.defaultMaxListeners;

Member attributes

url

String, query the server connected to the current object

1
readonly String WebSocket.url;

protocol

String, query the protocol when the current object is connected

1
readonly String WebSocket.protocol;

origin

String, query the source of the current object connection

1
readonly String WebSocket.origin;

readyState

Integer, query the connection status of the current object, see ws

1
readonly Integer WebSocket.readyState;

onopen

Function, query and bind connection success event, equivalent to on("open", func);

1
Function WebSocket.onopen;

onmessage

Function, query and bind the event of receiving the other party's message, equivalent to on("message", func);

1
Function WebSocket.onmessage;

onclose

Function, query and bind the event of connection closure, equivalent to on("close", func);

1
Function WebSocket.onclose;

onerror

Function, query and bind the error event, equivalent to on("error", func);

1
Function 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 2
WebSocket.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, the default is ""

send

Send a text to the other party

1
WebSocket.send(String data);

Call parameters:

  • data : String, specify the text sent

Send a piece of binary data to the other party

1
WebSocket.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

1
WebSocket WebSocket.ref();

Return result:

  • WebSocket , return the current object

unref

Allow the fibjs process to exit, and allow the fibjs process to exit during object binding

1
WebSocket WebSocket.unref();

Return result:

  • WebSocket , return the current object

on

Bind an event handler to the object

1 2
Object WebSocket.on(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Bind an event handler to the object

1
Object 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 processing function

Return result:

  • Object , returns the event object itself, easy to chain call

addListener

Bind an event handler to the object

1 2
Object WebSocket.addListener(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Bind an event handler to the object

1
Object WebSocket.addListener(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name will be the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependListener

Bind an event handler to the object start

1 2
Object WebSocket.prependListener(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Bind an event handler to the start of the object

1
Object 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 processing function

Return result:

  • Object , returns the event object itself, easy to chain call

once

Bind a one-time event processing function to the object, the one-time processing function will only be triggered once

1 2
Object WebSocket.once(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Bind a one-time event processing function to the object, the one-time processing function will only be triggered once

1
Object WebSocket.once(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name will be the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

prependOnceListener

Bind an event handler to the start of the object

1 2
Object WebSocket.prependOnceListener(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Bind an event handler to the object start

1
Object WebSocket.prependOnceListener(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name will be the event name, and the property value will be the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

off

Cancel the specified function from the object processing queue

1 2
Object WebSocket.off(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Cancel all functions in the object processing queue

1
Object WebSocket.off(String ev);

Call parameters:

  • ev : String, specify the name of the event

Return result:

  • Object , returns the event object itself, easy to chain call

Cancel the specified function from the object processing queue

1
Object WebSocket.off(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

removeListener

Cancel the specified function from the object processing queue

1 2
Object WebSocket.removeListener(String ev, Function func);

Call parameters:

  • ev : String, specify the name of the event
  • func : Function, specify event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

Cancel all functions in the object processing queue

1
Object WebSocket.removeListener(String ev);

Call parameters:

  • ev : String, specify the name of the event

Return result:

  • Object , returns the event object itself, easy to chain call

Cancel the specified function from the object processing queue

1
Object WebSocket.removeListener(Object map);

Call parameters:

  • map : Object, specify the event mapping relationship, the object property name is used as the event name, and the property value is used as the event processing function

Return result:

  • Object , returns the event object itself, easy to chain call

removeAllListeners

Cancel all listeners of all events from the object processing queue. If an event is specified, remove all listeners of the specified event.

1
Object WebSocket.removeAllListeners(Array evs = []);

Call parameters:

  • evs : Array, specify the name of the event

Return result:

  • Object , returns the event object itself, easy to chain call

setMaxListeners

The default limit of the number of listeners, only for compatibility

1
WebSocket.setMaxListeners(Integer n);

Call parameters:

  • n : Integer, specify the number of events

getMaxListeners

Get the default limit of the number of listeners, only for compatibility

1
Integer WebSocket.getMaxListeners();

Return result:

  • Integer , returns the default limit number

listeners

Array of listeners for the specified event of the query object

1
Array WebSocket.listeners(String ev);

Call parameters:

  • ev : String, specify 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

1
Integer WebSocket.listenerCount(String ev);

Call parameters:

  • ev : String, specify the name of the event

Return result:

  • Integer , returns the number of listeners for the specified event

eventNames

Query listener event name

1
Array WebSocket.eventNames();

Return result:

  • Array , returns an array of event names

emit

Actively trigger an event

1 2
Boolean 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 re-implemented according to its own characteristics

1
String WebSocket.toString();

Return result:

  • String , returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value WebSocket.toJSON(String key = "");

Call parameters:

  • key : String, unused

Return result:

  • Value , returns a value containing JSON serializable