Object built-in object

ObjectWebSocketMessage

WebSocketMessageyesWebSocketA message type in a protocol that encapsulatesWebSocketThe data format and processing methods of various messages in the transmission protocol can be used toWebSocketThe client and server communicate.

The constructor of the class WebSocketMessagesupports a specified message type parameter type, which has three optional values:

  • ws.TEXT: Represents a text type message, the content is a string.
  • ws.BINARY: Represents a binary type message, the content is binary data.

In addition, you can also WebSocketMessage.maskedspecify whether a mask needs to be applied by modifying the attribute, and WebSocketMessage.compresswhether compression is required by using the attribute.

The following code is an example of a websocket server. When a client connects, the server will echo the received message back to the client:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var ws = require('ws'); var http = require('http'); var svr = new http.Server(8080, { '/websocket': ws.upgrade((conn, req) => { // emit message event conn.onmessage = e => { if (e.data.type == ws.TEXT) { console.log(`接收到客户端发来的消息 ${e.data}`); conn.send(e.data); } else { console.error(`收到未知类型消息 ${e.data.type}`); } } conn.onclose = e => console.log('离开了一个客户端'); }) }); svr.start();

In this program, the websocket support module and the built-inhttpmodule and then createdhttpservice object, and specify the request path to be processed, callingws.upgradeThe function upgrades the request of the corresponding path to a websocket connection. After creating a websocket connection, the server will automatically create aws.SocketObject, and provides onopen, onmessage, onclose and other APIs to handle when a client connects, receives a message and closes events. When receiving a message, the server will determine the type of the message. If it is a text type, it will echo the received message back. The above is a simple websocket server processing flow, which can be modified according to actual needs.

inheritance relationship

Constructor

WebSocketMessage

Package handling message object constructor

1 2 3 4
new WebSocketMessage(Integer type = ws.BINARY, Boolean masked = true, Boolean compress = false, Integer maxSize = 67108864);

Call parameters:

  • type: Integer, websocket message type, default is websocket.BINARY
  • masked: Boolean, websocket message mask, default is true
  • compress: Boolean, marks whether the message is compressed, the default is false
  • maxSize: Integer, maximum packet size in MB, default is 67108864 (64M)

constant

TEXT

Specify message type 1, which represents a text type

1
const WebSocketMessage.TEXT = 1;

BINARY

Specify message type 2, representing a binary type

1
const WebSocketMessage.BINARY = 2;

member properties

masked

Boolean, query and read websocket mask flag, default is true

1
Boolean WebSocketMessage.masked;

compress

Boolean, query and read websocket compression status, default is false

1
Boolean WebSocketMessage.compress;

maxSize

Integer, query and set the maximum packet size in bytes, the default is 67108864 (64M)

1
Integer WebSocketMessage.maxSize;

value

String, the basic content of the message

1
String WebSocketMessage.value;

params

NArray, the basic parameters of the message

1
readonly NArray WebSocketMessage.params;

type

Integer, message type

1
Integer WebSocketMessage.type;

data

Value, query the data of the message. This attribute will return different data according to the content-type. When it is text, the text will be returned.jsonreturn whenjson, otherwise it returns binary

1
readonly Value WebSocketMessage.data;

body

SeekableStream, a stream object containing the data portion of the message

1
SeekableStream WebSocketMessage.body;

length

Long, the length of the message data part

1
readonly Long WebSocketMessage.length;

stream

Stream, the stream object when querying the message readFrom

1
readonly Stream WebSocketMessage.stream;

lastError

String, query and set the last error of message processing

1
String WebSocketMessage.lastError;

member function

read

Read data of specified size from the stream. This method is an alias of the corresponding method of body.

1
Buffer WebSocketMessage.read(Integer bytes = -1) async;

Call parameters:

  • bytes: Integer, specifies the amount of data to be read. The default is to read data blocks of random size. The size of the data read depends on the device.

Return results:

  • Buffer, returns the data read from the stream. If there is no data to read or the connection is interrupted, null is returned.

readAll

Read all remaining data from the stream. This method is an alias of the corresponding method of body.

1
Buffer WebSocketMessage.readAll() async;

Return results:

  • Buffer, returns the data read from the stream. If there is no data to read or the connection is interrupted, null is returned.

write

Write the given data. This method is an alias of the corresponding method in body.

1
WebSocketMessage.write(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to be written

json

Writes the given data in JSON encoding

1
Value WebSocketMessage.json(Value data);

Call parameters:

  • data: Value, given the data to be written

Return results:

  • Value, this method does not return data

Parse the data in the message as JSON encoding

1
Value WebSocketMessage.json();

Return results:

  • Value, returns the parsed result

pack

bymsgpackEncoding writes the given data

1
Value WebSocketMessage.pack(Value data);

Call parameters:

  • data: Value, given the data to be written

Return results:

  • Value, this method does not return data

bymsgpackEncoding and parsing the data in the message

1
Value WebSocketMessage.pack();

Return results:

  • Value, returns the parsed result

end

Set the end of current message processing,ChainThe processor does not continue with subsequent transactions

1
WebSocketMessage.end();

isEnded

Query whether the current message has ended

1
Boolean WebSocketMessage.isEnded();

Return results:

  • Boolean, returns true when finished

clear

Clear message content

1
WebSocketMessage.clear();

sendTo

Sends a formatted message to the given stream object

1
WebSocketMessage.sendTo(Stream stm) async;

Call parameters:

  • stm:Stream, specifies the stream object that receives the formatted message

readFrom

Reads the formatted message from the given cache stream object and parses the populated object

1
WebSocketMessage.readFrom(Stream stm) async;

Call parameters:

  • stm:Stream, specifies the stream object for reading formatted messages

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String WebSocketMessage.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.

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

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable