Skip to content

Commit 61becc0

Browse files
committed
合并server端和www
1 parent e456933 commit 61becc0

File tree

8 files changed

+50
-73
lines changed

8 files changed

+50
-73
lines changed
File renamed without changes.

README.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,19 @@ demo演示:https://fagedongxi.com
1313
## 场景:
1414
比如新装的win系统需要从mac系统传一些需要🪜才能下载的软件或者搜到的一些东西
1515

16-
## 服务端部署(仅部署服务端不行,一定看到最后的“网页部署”):
17-
部署介绍:https://v.douyin.com/iUWewPmf/
16+
## 部署/启动
17+
> `1.1.0`版本后,不再需要单独部署网页端了,仅启动一个服务端即可
1818
1919
### 源码方式
2020
1. 安装nodejs,node版本没有测试,我用的是 `16.20.2`
21-
2. 下载源码(服务端仅需要`server`目录)
22-
3. 进入 `server` 目录,运行 `npm install`
21+
2. 下载源码
22+
3. 进入 项目根目录,运行 `npm install`
2323
4. 运行 `npm run start [port]` ,例如 `npm run start 8081`
2424

2525
### 二进制方式
2626
* 下载对应平台的可执行文件,直接执行即可(服务端)
2727
* 默认监听 `8081` 端口,可通过参数指定端口,例如 `./internal-chat-linux 8082`
2828
* 如果你用windows,可参考 https://v.douyin.com/CeiJahpLD/ 注册成服务
2929

30-
### 服务端nginx反向代理配置参考(可选)
31-
> 服务端用反向代理的好处:可以直接用certbot申请https证书,然后直接用wss协议。
32-
> 如果采用下方的配置反向代理,注意在客户端配置`wsUrl`变量的时候,需要加 `/ws`,否则不用
33-
```
34-
location /ws/ {
35-
proxy_pass http://localhost:8081/;
36-
proxy_http_version 1.1;
37-
proxy_set_header Host $host;
38-
proxy_set_header Upgrade $http_upgrade;
39-
proxy_set_header Connection "upgrade";
40-
proxy_set_header X-Real-IP $remote_addr;
41-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
42-
}
43-
```
44-
45-
## 网页部署:
46-
1. 下载源码并修改`www/index.js`第一行代码`wsUrl`变量(如果服务端配置了反向代理,这里路径最后要加`/ws`,否则不用)
47-
2. 直接将`www`用nginx部署成一个静态网站即可,具体配置参考 `nginxvhost.conf`。如果你没有域名,将 `server_name` 写成 `_` 即可(属于nginx基础知识)
48-
3. 访问 `http://your.domain.com/` 即可
49-
5030
## 免责声明:
5131
本项目仅用于学习交流,请勿用于非法用途,否则后果自负。
File renamed without changes.

server/index.js renamed to index.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const WebSocket = require('ws');
22
const service = require('./data');
33
const path = require('path');
44

5+
const http = require('http');
6+
const fs = require('fs');
7+
58
const originalLog = console.log;
69
console.log = function() {
710
const date = new Date();
@@ -12,10 +15,39 @@ console.log = function() {
1215

1316
originalLog.apply(console, [`[${timestamp}]`, ...arguments]);
1417
};
18+
const HTTP_PORT = process.argv[2] || 8081; // 合并后的统一端口
19+
const HTTP_DIRECTORY = path.join(__dirname, 'www'); // 静态文件目录
20+
21+
// 创建 HTTP 服务器
22+
const server = http.createServer((req, res) => {
23+
let urlPath = decodeURIComponent(req.url.split('?')[0]); // 去掉查询参数
24+
if (urlPath === '/') {
25+
urlPath = '/index.html'; // 默认访问 index.html
26+
}
27+
let filePath = path.join(HTTP_DIRECTORY, urlPath);
28+
fs.stat(filePath, (err, stats) => {
29+
if (err || !stats.isFile()) {
30+
// 如果文件不存在,返回 index.html
31+
filePath = path.join(HTTP_DIRECTORY, 'index.html');
32+
}
33+
34+
// 设置缓存头
35+
const ext = path.extname(filePath);
36+
if (ext === '.js' || ext === '.css') {
37+
res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30天缓存
38+
}
39+
40+
fs.createReadStream(filePath).pipe(res);
41+
});
42+
});
43+
44+
server.listen(HTTP_PORT, () => {
45+
console.log(`server start on port ${HTTP_PORT}`);
46+
});
47+
48+
49+
const wsServer = new WebSocket.Server({ server });
1550

16-
// 接收启动参数作为端口号,默认8081
17-
const PORT = process.argv[2] || 8081;
18-
const server = new WebSocket.Server({ port: PORT });
1951

2052
const SEND_TYPE_REG = '1001'; // 注册后发送用户id
2153
const SEND_TYPE_ROOM_INFO = '1002'; // 发送房间信息
@@ -44,15 +76,13 @@ try {
4476
});
4577
console.log(`加载房间数据: ${roomIds.join(',')}`);
4678
} catch (e) {
47-
console.error('Failed to load room_pwd.json');
79+
// 没有room_pwd.json文件无需报错,不加载即可
80+
// console.error('Failed to load room_pwd.json');
4881
}
4982

50-
console.log(`Signaling server running on ws://localhost:${PORT}`);
51-
52-
server.on('connection', (socket, request) => {
83+
wsServer.on('connection', (socket, request) => {
5384
const ip = request.headers['x-forwarded-for'] ?? request.headers['x-real-ip'] ?? socket._socket.remoteAddress.split("::ffff:").join("");
54-
55-
const urlWithPath = request.url.split('/')
85+
const urlWithPath = request.url.replace(/^\//g, '').split('/')
5686
let roomId = null;
5787
let pwd = null;
5888
if (urlWithPath.length > 1 && urlWithPath[1].length > 0 && urlWithPath[1].length <= 32) {

nginxvhost.conf

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "internal-chat",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -15,7 +15,8 @@
1515
"pkg": {
1616
"assets": [
1717
"!room_pwd.json",
18-
"./node_modules/**/*"
18+
"./node_modules/**/*",
19+
"./www/**/*"
1920
],
2021
"ignore": [
2122
"./room_pwd.json"
@@ -24,6 +25,7 @@
2425
"node16-linux-x64",
2526
"node16-win-x64",
2627
"node16-macos-x64"
27-
]
28+
],
29+
"outputPath": "dist"
2830
}
2931
}

www/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const wsUrl = 'wss://fagedongxi.com/ws';
1+
const wsProtocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
2+
const wsUrl = `${wsProtocol}://${window.location.hostname}${window.location.port ? `:${window.location.port}` : ''}/ws`;
23

34
var users = [];
45
var me = new XChatUser();

0 commit comments

Comments
 (0)