Skip to content

Commit 277f7e4

Browse files
修复 event 案例错误,为 redirect 添加重定向配置
1 parent 858ea9f commit 277f7e4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

examples/js_ws_event/event.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ async function eventPull() {
1212
await ws.writeText(data);
1313
}
1414
}
15-
1615
async function messagePull() {
1716
while (true) {
1817
const data = await ws.readText()
19-
if (data === "exit") break;
18+
if (data === "exit")
19+
await event.put("messages", JSON.stringify({
20+
name:name,
21+
data: name+' 已断开连接'
22+
}));
23+
break;
2024
if (data?.trim()) {
2125
await event.put("messages", JSON.stringify({
2226
name:name,
@@ -27,5 +31,5 @@ async function messagePull() {
2731
}
2832

2933
(async () => {
30-
await Promise.all([eventPull(), messagePull()])
34+
await Promise.any([eventPull(), messagePull()])
3135
})()

pkg/filters/redirect.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ import (
1515

1616
var portExp = regexp.MustCompile(`:\d+$`)
1717

18-
func FilterInstRedirect(_ core.Params) (core.FilterInstance, error) {
18+
func FilterInstRedirect(g core.Params) (core.FilterInstance, error) {
19+
var global struct {
20+
Scheme string `json:"scheme"`
21+
}
22+
if err := g.Unmarshal(&global); err != nil {
23+
return nil, err
24+
}
25+
if global.Scheme == "" {
26+
global.Scheme = "https"
27+
}
1928
return func(config core.Params) (core.FilterCall, error) {
2029
var param struct {
2130
Targets []string `json:"targets"`
@@ -42,12 +51,11 @@ func FilterInstRedirect(_ core.Params) (core.FilterInstance, error) {
4251
if strings.HasSuffix(path, "/index.html") || path == "index.html" {
4352
path = strings.TrimSuffix(path, "index.html")
4453
}
45-
target, err := url.Parse(fmt.Sprintf("https://%s/%s", param.Targets[0], path))
54+
target, err := url.Parse(fmt.Sprintf("%s://%s/%s", global.Scheme, param.Targets[0], path))
4655
if err != nil {
4756
return err
4857
}
4958
target.RawQuery = request.URL.RawQuery
50-
5159
http.Redirect(writer, request, target.String(), param.Code)
5260
return nil
5361
}

0 commit comments

Comments
 (0)