I am currently using Node.js to handle the back-end of my website but I am unsure of how Websockets/Objects are handled together.
This is a template I am using as an example of my main class. (Sends web-requests to a specific page)
JavaScript
x
35
35
1
class ViewClass {
2
constructor(URL, views) {
3
this.link = URL;
4
this.views = views;
5
this.make_requests();
6
}
7
8
make_requests() {
9
try {
10
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
11
const xhr = new XMLHttpRequest();
12
13
let link = this.link;
14
let views = this.views;
15
16
for (let index = 1; index < views + 1; index++) {
17
xhr.open("GET", link, false);
18
19
xhr.onload = function (e) {
20
if (xhr.readyState === 4) {
21
if (xhr.status === 200) {
22
console.log("View: " + index + " Sent Successfully!");
23
} else {
24
console.error("View: " + index + " Failed!");
25
}
26
}
27
};
28
29
xhr.send(null);
30
}
31
} catch (error) {
32
console.log(error.message);
33
}
34
}
35
}
This is my Main Websocket File (Stripped for simplicity)
JavaScript
1
13
13
1
server.on('connection', function (socket) {
2
console.log("Welcomed Connection from: " + socket.remoteAddress);
3
4
socket.on('close', function (resp) {
5
console.log(`[${GetDate(3)}] Bye!`);
6
});
7
8
socket.on('data', function (buf) {
9
// Take Views/URL from Front-end.
10
// Initialise a new Object from ViewClass and let it run until finished.
11
});
12
});
13
Lets say I receive data from the WebSocket and that data creates a new ViewClass object and starts running immediately. Will that Now Running code block the input/output of the Node.js Server? Or will it be handled in the background?
If there is any information I can provide to make it clearer let me know as I am extremely new to Websocket/Js and I am more than likely missing information.