From 6da58473d88394dd8b041318257a6e8071ac59d8 Mon Sep 17 00:00:00 2001 From: tursom Date: Sun, 28 Apr 2024 11:19:15 +0800 Subject: [PATCH] export chat service listen host --- chat.js | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/chat.js b/chat.js index ffedb1e..2f566e7 100644 --- a/chat.js +++ b/chat.js @@ -4,33 +4,37 @@ const http = require('http'); const {once} = require("node:events"); const server = http.createServer(async (req, res) => { - let body = ''; - req.on('data', chunk => { - body += chunk.toString(); // 将Buffer转换为字符串 - }); + try { + let body = ''; + req.on('data', chunk => { + body += chunk.toString(); // 将Buffer转换为字符串 + }); - await once(req, 'end'); + await once(req, 'end'); - let requests = JSON.parse(body); + let requests = JSON.parse(body); - console.log(requests.id) - console.log(requests.msg) + console.log(requests.id) + console.log(requests.msg) - client.steamUser.chat.sendFriendMessage(requests.id, requests.msg, (err, response) => { - if (err) { - console.log(err); - res.statusCode = 500; + client.steamUser.chat.sendFriendMessage(requests.id, requests.msg, (err, response) => { + if (err) { + console.log(err); + res.statusCode = 500; + res.setHeader('Content-Type', 'text/plain'); + res.end('Internal Server Error\n'); + return; + } + + res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); - res.end('Internal Server Error\n'); - return; - } - - res.statusCode = 200; - res.setHeader('Content-Type', 'text/plain'); - res.end('Success\n'); - }) + res.end('Success\n'); + }) + } catch (e) { + console.error("An error occurred while processing the request: ", e); + } }); -server.listen(3000, '127.0.0.1', () => { - console.log('Server running at http://127.0.0.1:3000/'); +server.listen(3000, '0.0.0.0', () => { + console.log('Server running at http://0.0.0.0:3000/'); });