Kano001 commited on
Commit
f5fc3f3
·
verified ·
1 Parent(s): 497f971

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +15 -33
server.js CHANGED
@@ -1,37 +1,19 @@
1
- const express = require('express');
2
- const bodyparser = require('body-parser');
3
- const txt2image = require("./txt2images")
4
- const fs = require('fs'); //引入fs模块
5
 
6
- // Constants
7
- const PORT = 7860;
8
- const HOST = '0.0.0.0';
9
 
10
- // App
11
- const app = express();
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
- app.listen(PORT, HOST, () => {
31
- if (HOST == '0.0.0.0') {
32
- console.log(`Running on http://127.0.0.1:${PORT}`);
33
- } else {
34
- console.log(`Running on http://${HOST}:${PORT}`);
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');