98 lines
7.2 KiB
Markdown
98 lines
7.2 KiB
Markdown
### HTTP Server Request Handling
|
|
|
|
The provided code snippet demonstrates the request handling logic of an HTTP server. Below is an explanation of the code's functionality:
|
|
|
|
#### Functionality:
|
|
|
|
- The code creates an HTTP server using `http.createServer((req, res) => {...})`, defining a callback function to handle incoming requests.
|
|
- The server sets the "Access-Control-Allow-Origin" header to allow requests from any origin (`*`).
|
|
- The server distinguishes between HTTP GET requests and other request methods.
|
|
- For GET requests, the server parses the query parameters from the request URL using the `url.parse(req.url, true)` method.
|
|
- If the URL does not contain search parameters, the server responds with an empty body.
|
|
- If search parameters are present, the server processes the request using `client.processRequestFromUser(u.query)`.
|
|
- If processing is successful, the server sends the response as a JSON object using `res.end(JSON.stringify(result[0]))`.
|
|
- If an error occurs during processing:
|
|
- If the error is an instance of `INVALID`, the server responds with the error message and the corresponding HTTP status code.
|
|
- For other errors, the server logs the error, responds with an internal error message, and sets the appropriate HTTP status code.
|
|
|
|
#### Request Handling Logic:
|
|
|
|
- The server's primary function is to handle GET requests containing query parameters.
|
|
- It processes the query parameters and communicates with the `client` module to generate a response.
|
|
- Successful responses are sent back to the client in JSON format, providing the requested data.
|
|
- Error handling ensures that appropriate error messages and status codes are returned to the client, enhancing the server's reliability and user experience.
|
|
|
|
This HTTP server logic allows clients to request specific data via GET requests and receive tailored responses, enabling seamless interaction between clients and the server.
|
|
|
|
### HTTP Server POST Request Handling
|
|
|
|
POST Request handles incoming HTTP POST requests. H
|
|
|
|
#### Functionality:
|
|
|
|
- The code checks if the incoming request method is "POST".
|
|
- For POST requests, the server processes the data sent in the request body.
|
|
- The server listens for data chunks and assembles them into a complete data string using the `req.on('data', chunk => data += chunk)` event.
|
|
- Once all data is received (`req.on('end', ...) event`), the server processes the data using `client.processIncomingData(data)`.
|
|
- If processing is successful, the server sends the response as a JSON object using `res.end(JSON.stringify(result[0]))`.
|
|
- The response data includes the processed information.
|
|
- If a processing mode (`result[1]`) is provided, specific actions are taken based on the mode:
|
|
- If the mode is 'DATA', 'EDIT', 'TAG', or 'NOTE', the processed data is sent to live requests using `sendToLiveRequests(result[0])`.
|
|
- Additionally, the processed data is forwarded to the next node in the network using `intra.forwardToNextNode(result[1], result[0])`.
|
|
- The server also decrements the countdown timer using `refresher.countdown`.
|
|
- If an error occurs during processing:
|
|
- If the error is an instance of `INVALID`, the server responds with the error message and the corresponding HTTP status code.
|
|
- For other errors, the server logs the error, responds with an internal error message, and sets the appropriate HTTP status code.
|
|
|
|
#### Request Handling Logic:
|
|
|
|
- The server's POST request handling logic focuses on processing incoming data.
|
|
- Data sent via POST requests is processed, allowing various modes of operation ('DATA', 'EDIT', 'TAG', 'NOTE') to be executed based on the content of the request.
|
|
- Processed data is sent to live requests and forwarded to the next node, facilitating real-time data synchronization and network communication.
|
|
- Error handling ensures that appropriate error messages and status codes are returned to the client, enhancing the server's reliability and user experience.
|
|
|
|
This POST request handling logic enables clients to send data to the server, trigger specific actions, and receive real-time updates, enhancing the server's interactive capabilities and data synchronization functionalities.
|
|
|
|
|
|
|
|
### WebSocket Server Functionality
|
|
|
|
The provided code snippet demonstrates the functionality of a WebSocket server that handles incoming messages from clients. Here's an overview of how the server processes messages without including initialization details:
|
|
|
|
#### Connection Handling and Message Processing:
|
|
|
|
- When a client establishes a connection, the server listens for incoming messages on that connection.
|
|
- Upon receiving a message, the server checks if it starts with `intra.SUPERNODE_INDICATOR`.
|
|
- If it does, the server processes the message as a task from a supernode using `intra.processTaskFromSupernode(message, ws)`.
|
|
- If not, the server treats the message as a JSON object sent by a user.
|
|
- The server attempts to parse the incoming JSON request.
|
|
- If the request contains a `status` field, indicating a status update from the user, the server processes it using `client.processStatusFromUser(request, ws)`.
|
|
- If the request doesn't contain a `status` field, it is treated as a user request. The server processes the request using `client.processRequestFromUser(request)`.
|
|
- If processing is successful, the server sends the response back to the user via the WebSocket connection (`ws.send(JSON.stringify(result[0]))`).
|
|
- Errors encountered during processing are handled. If configured (`floGlobals.sn_config.errorFeedback`), appropriate error messages are sent back to the user.
|
|
|
|
#### Error Handling:
|
|
- If the incoming message is not in valid JSON format, the server sends an error message indicating that the request is not in JSON format.
|
|
|
|
This server setup allows bidirectional communication between clients and the server, enabling the processing of tasks and user requests in a real-time manner.
|
|
|
|
### Broadcasting Data to Live Requests
|
|
|
|
The provided function `sendToLiveRequests(data)` broadcasts data to all live WebSocket requests based on certain conditions. Here's an explanation of how this function works:
|
|
|
|
#### Functionality:
|
|
|
|
- The function takes a `data` parameter, representing the data to be sent to live WebSocket requests.
|
|
- It iterates over all connected WebSocket clients stored in the `wsServer.clients` set.
|
|
- For each client, it checks if the client's active request (stored in `ws._liveReq`) satisfies specific conditions using `client.checkIfRequestSatisfy(ws._liveReq, data)`.
|
|
- If the client's request matches the conditions, the function sends the `data` to that client in JSON format using `ws.send(JSON.stringify(data))`.
|
|
|
|
#### Broadcasting Logic:
|
|
|
|
- The function acts as a broadcast mechanism, allowing targeted messages to be sent to specific clients based on their active requests.
|
|
- The conditions specified in `client.checkIfRequestSatisfy(ws._liveReq, data)` determine which clients receive the broadcasted data.
|
|
- Clients with active requests that meet the specified conditions will receive the `data` object.
|
|
|
|
This functionality enables real-time communication with clients by delivering tailored data updates to specific active requests, enhancing the efficiency and responsiveness of the WebSocket server.
|
|
|