rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
41 lines (40 loc) • 1.24 kB
JavaScript
import WsOpenContext from "./WsOpenContext";
export default class WsMessageContext extends WsOpenContext {
constructor(context, rawContext, abort) {
super(context, rawContext, abort, 'message');
}
/**
* The Websocket Message (JSON Automatically parsed if enabled)
* @since 5.4.0
*/ message() {
const stringified = this.context.body.raw.toString();
if ((stringified.startsWith('{') && stringified.endsWith('}')) || (stringified.startsWith('[') && stringified.endsWith(']'))) {
try {
const json = JSON.parse(stringified);
this.context.body.parsed = json;
}
catch {
this.context.body.parsed = stringified;
}
}
return this.context.body.parsed;
}
/**
* The Websocket Message Type
* @since 9.0.0
*/ messageType() {
return this.rawContext.messageType();
}
/**
* The Raw Websocket Message
* @since 5.5.2
*/ rawMessage(encoding) {
return this.context.body.raw.toString(encoding);
}
/**
* The Raw Socket Message as Buffer
* @since 8.1.4
*/ rawMessageBytes() {
return this.context.body.raw;
}
}