Update server.js
Browse files
server.js
CHANGED
@@ -1,37 +1,19 @@
|
|
1 |
-
const
|
2 |
-
const bodyparser = require('body-parser');
|
3 |
-
const txt2image = require("./txt2images")
|
4 |
-
const fs = require('fs'); //引入fs模块
|
5 |
|
6 |
-
|
7 |
-
const PORT = 7860;
|
8 |
-
const HOST = '0.0.0.0';
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
app.use(bodyparser.urlencoded({ extended: true }))
|
13 |
-
app.post("/txt2pic", async (req, res) => {
|
14 |
-
var prompt = req.body.prompt;
|
15 |
-
var size = req.body.size;
|
16 |
-
var url = await txt2image.txt2img(prompt, size)
|
17 |
-
res.send(url)
|
18 |
-
})
|
19 |
-
app.get('/', (req, res) => {
|
20 |
-
res.writeHead(200, { 'Content-Type': 'text/html' })
|
21 |
-
var html = fs.readFileSync(__dirname + '/static/index.html', 'utf-8'); //读取html文件,__dirname表示当前文件所在的目录路径
|
22 |
-
res.end(html);
|
23 |
-
});
|
24 |
-
app.get('/jquery', (req, res) => {
|
25 |
-
res.writeHead(200, { "Content-Type": "application/javascript" })
|
26 |
-
var js = fs.readFileSync(__dirname + '/static/jquery.js', 'utf-8'); //读取jquery文件,__dirname表示当前文件所在的目录路径
|
27 |
-
res.end(js);
|
28 |
-
});
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
1 |
+
const WebSocket = require('ws');
|
|
|
|
|
|
|
2 |
|
3 |
+
const wss = new WebSocket.Server({ port: 7860 });
|
|
|
|
|
4 |
|
5 |
+
wss.on('connection', function connection(ws) {
|
6 |
+
console.log('A new client connected!');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
ws.on('message', function incoming(message) {
|
9 |
+
console.log('Received:', message);
|
10 |
+
// Echo the message back to the client
|
11 |
+
ws.send(`Server received: ${message}`);
|
12 |
+
});
|
13 |
+
|
14 |
+
ws.on('close', function () {
|
15 |
+
console.log('Client has disconnected');
|
16 |
+
});
|
17 |
+
});
|
18 |
|
19 |
+
console.log('WebSocket server is listening on ws://localhost:8080');
|