Javascript class 网络套接字过早停止执行
Javascript class with web socket prematurely ceasing execution
我正在尝试构建一个 JavaScript class,它允许我通过网络套接字(ws 库)与我的 Home Assistant 服务器进行交互。脚本旨在 node.js环境。
const WebSocket = require('ws');
class HomeAssistantWebSocket {
constructor(config = {}) {
this.config = config;
this.initialize();
}
config;
initialized = false;
initializeErrors = [];
authenticated = false;
ws = null;
authenticate = () => {
let {
config,
ws,
serialize
} = this;
console.log("Attempting to authenticate...");
ws.send(serialize({
"type": "auth",
"access_token": config["access_token"]
}));
return true;
}
openConnection = () => {
let {
ws
} = this;
ws.on('open', () => {
console.log("Connection opened!");
});
ws.on('message', (data) => {
this.handleMessage(data);
});
}
deserialize = (string) => {
try {
return JSON.parse(string);
} catch (error) {
return false;
}
}
handleMessage = (data) => {
let {
authenticate,
deserialize,
ws
} = this;
data = deserialize(data);
console.log(data);
if(data["type"] === "auth_required") {
authenticate();
}
if (data["type"] === "auth_ok" && !this.authenticated) {
this.authenticated = true;
console.log("Successfully authenticated");
}
if (data["type"] === "auth_ok") {
ws.send(JSON.stringify({
"id": 20,
"type": "subscribe_events",
}));
}
}
initialize = () => {
let {
config,
initialized,
initializeErrors,
} = this;
if (Object.keys(config).length < 1) {
initializeErrors.push("No config present.");
} else if (!config.hasOwnProperty("access_token") && typeof config["access_token"] === "string") {
initializeErrors.push("Config must contain a valid access_token.");
} else if (!config.hasOwnProperty("home_assistant_url") && typeof config["home_assistant_url"] === "string") {
initializeErrors.push("Config must contain a valid home_assistant_url");
}
if (this.initializeErrors.length === 0) {
this.ws = new WebSocket(config["home_assistant_url"]);
this.openConnection();
initialized = true;
console.log("Attempting to open connection...");
} else {
console.log("Failed to initialize:");
this.initializeErrors.forEach((e) => {
console.log(e);
});
}
return true;
}
serialize = (json) => {
try {
return JSON.Stringify(json);
} catch (error) {
return false;
}
}
}
const haWS = new HomeAssistantWebSocket({
"access_token": "redacted_access_token",
"home_assistant_url": "ws://homeassistant.local:8123/api/websocket"
});
我 运行 遇到了一个问题,我的代码在身份验证阶段后停止执行。我的代码在控制台中打印以下内容,然后脚本停止执行。没有错误。
Connection opened!
{ type: 'auth_required', ha_version: '2021.2.3' }
Attempting to authenticate...
我已验证我的代码确实正确连接到网络套接字 api 并且正在与家庭助理服务器通信。有没有人看到我的代码有什么问题会导致脚本停止 execution/garbage 收集 ws on 消息以防止收到更多消息?
我有一个非常基本的示例,它在 class 之外按预期工作,使其通过身份验证阶段并保持套接字打开并按预期接收数据。任何帮助将不胜感激。
serialize = (json) => {
try {
return JSON.stringify(json);
} catch (error) {
return false;
}
}
我在序列化函数中发现了问题。我对 JSON.stringify 函数的引用不正确。在我的代码中它是 JSON.Stringify。应该是 JSON.stringify.
总是小事...
我正在尝试构建一个 JavaScript class,它允许我通过网络套接字(ws 库)与我的 Home Assistant 服务器进行交互。脚本旨在 node.js环境。
const WebSocket = require('ws');
class HomeAssistantWebSocket {
constructor(config = {}) {
this.config = config;
this.initialize();
}
config;
initialized = false;
initializeErrors = [];
authenticated = false;
ws = null;
authenticate = () => {
let {
config,
ws,
serialize
} = this;
console.log("Attempting to authenticate...");
ws.send(serialize({
"type": "auth",
"access_token": config["access_token"]
}));
return true;
}
openConnection = () => {
let {
ws
} = this;
ws.on('open', () => {
console.log("Connection opened!");
});
ws.on('message', (data) => {
this.handleMessage(data);
});
}
deserialize = (string) => {
try {
return JSON.parse(string);
} catch (error) {
return false;
}
}
handleMessage = (data) => {
let {
authenticate,
deserialize,
ws
} = this;
data = deserialize(data);
console.log(data);
if(data["type"] === "auth_required") {
authenticate();
}
if (data["type"] === "auth_ok" && !this.authenticated) {
this.authenticated = true;
console.log("Successfully authenticated");
}
if (data["type"] === "auth_ok") {
ws.send(JSON.stringify({
"id": 20,
"type": "subscribe_events",
}));
}
}
initialize = () => {
let {
config,
initialized,
initializeErrors,
} = this;
if (Object.keys(config).length < 1) {
initializeErrors.push("No config present.");
} else if (!config.hasOwnProperty("access_token") && typeof config["access_token"] === "string") {
initializeErrors.push("Config must contain a valid access_token.");
} else if (!config.hasOwnProperty("home_assistant_url") && typeof config["home_assistant_url"] === "string") {
initializeErrors.push("Config must contain a valid home_assistant_url");
}
if (this.initializeErrors.length === 0) {
this.ws = new WebSocket(config["home_assistant_url"]);
this.openConnection();
initialized = true;
console.log("Attempting to open connection...");
} else {
console.log("Failed to initialize:");
this.initializeErrors.forEach((e) => {
console.log(e);
});
}
return true;
}
serialize = (json) => {
try {
return JSON.Stringify(json);
} catch (error) {
return false;
}
}
}
const haWS = new HomeAssistantWebSocket({
"access_token": "redacted_access_token",
"home_assistant_url": "ws://homeassistant.local:8123/api/websocket"
});
我 运行 遇到了一个问题,我的代码在身份验证阶段后停止执行。我的代码在控制台中打印以下内容,然后脚本停止执行。没有错误。
Connection opened!
{ type: 'auth_required', ha_version: '2021.2.3' }
Attempting to authenticate...
我已验证我的代码确实正确连接到网络套接字 api 并且正在与家庭助理服务器通信。有没有人看到我的代码有什么问题会导致脚本停止 execution/garbage 收集 ws on 消息以防止收到更多消息?
我有一个非常基本的示例,它在 class 之外按预期工作,使其通过身份验证阶段并保持套接字打开并按预期接收数据。任何帮助将不胜感激。
serialize = (json) => {
try {
return JSON.stringify(json);
} catch (error) {
return false;
}
}
我在序列化函数中发现了问题。我对 JSON.stringify 函数的引用不正确。在我的代码中它是 JSON.Stringify。应该是 JSON.stringify.
总是小事...