- CHANGELOG.md:v2.8.0 条目(中英双语); - README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明); - doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制); - skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/ require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过); - 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
97 lines
2.4 KiB
Go
Executable File
97 lines
2.4 KiB
Go
Executable File
package ws
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
const (
|
|
maxMessageBytes = 1 << 20
|
|
maxMessages = 256
|
|
)
|
|
|
|
// Connect dials url, sends one text message, collects text replies until close or timeout.
|
|
// Mirrors Rust ws::connect; errors are returned as {ok:false, error}.
|
|
func Connect(url, message string, headers map[string]any, timeoutSec uint64) map[string]any {
|
|
if !(strings.HasPrefix(url, "ws://") || strings.HasPrefix(url, "wss://")) {
|
|
return map[string]any{"ok": false, "error": "ws url must start with ws:// or wss://"}
|
|
}
|
|
if timeoutSec < 1 {
|
|
timeoutSec = 1
|
|
}
|
|
timeout := time.Duration(timeoutSec) * time.Second
|
|
hdr := http.Header{}
|
|
for k, v := range headersFromValue(headers) {
|
|
hdr.Set(k, v)
|
|
}
|
|
dialer := websocket.Dialer{HandshakeTimeout: timeout}
|
|
conn, _, err := dialer.Dial(url, hdr)
|
|
if err != nil {
|
|
return map[string]any{"ok": false, "error": fmt.Sprintf("ws connect: %v", err)}
|
|
}
|
|
defer conn.Close()
|
|
conn.SetReadLimit(maxMessageBytes)
|
|
|
|
if err := conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
|
|
return map[string]any{"ok": false, "error": fmt.Sprintf("ws send: %v", err)}
|
|
}
|
|
if err := conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
|
|
return map[string]any{"ok": false, "error": fmt.Sprintf("ws send: %v", err)}
|
|
}
|
|
|
|
var messages []string
|
|
deadline := time.Now().Add(timeout)
|
|
for {
|
|
if err := conn.SetReadDeadline(deadline); err != nil {
|
|
break
|
|
}
|
|
mt, data, err := conn.ReadMessage()
|
|
if err != nil {
|
|
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
|
|
break
|
|
}
|
|
return map[string]any{"ok": false, "error": fmt.Sprintf("ws receive: %v", err)}
|
|
}
|
|
if mt == websocket.TextMessage {
|
|
if len(messages) == maxMessages {
|
|
return map[string]any{"ok": false, "error": "ws receive: too many messages"}
|
|
}
|
|
messages = append(messages, string(data))
|
|
}
|
|
if mt == websocket.CloseMessage {
|
|
break
|
|
}
|
|
}
|
|
return map[string]any{"ok": true, "messages": toAnyList(messages)}
|
|
}
|
|
|
|
func headersFromValue(headers map[string]any) map[string]string {
|
|
out := map[string]string{}
|
|
if headers == nil {
|
|
return out
|
|
}
|
|
for k, v := range headers {
|
|
switch t := v.(type) {
|
|
case string:
|
|
out[k] = t
|
|
case nil:
|
|
out[k] = ""
|
|
default:
|
|
out[k] = fmt.Sprint(t)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func toAnyList(ss []string) []any {
|
|
out := make([]any, len(ss))
|
|
for i, s := range ss {
|
|
out[i] = s
|
|
}
|
|
return out
|
|
}
|