如何在成功连接 websocket 后向特定 URL 发送消息?

How to make send message to particular URI after successfull webscoket connection?

我在 localhost:443/server-demo(jetty websocket 服务器)上有一个安全的 websocket 服务器 运行。 现在我正在编写一个可以与 websocket 服务器通信的 go 客户端。我能够使用正确的证书连接到 websocket 服务器。这是示例代码。

package main
import (
    "crypto/tls"
    "crypto/x509"
    "fmt"
    "io"
    "log"
)
func main() {
    cert, err := tls.LoadX509KeyPair("nifi-1.10.0-bin/nifi-1.10.0/extras/gen-certs/certs/admin.pem", "nifi-1.10.0-bin/nifi-1.10.0/extras/gen-certs/certs/admin-key.pem")
    if err != nil {
        log.Fatalf("server: loadkeys: %s", err)
    }
    config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
    conn, err := tls.Dial("tcp", "127.0.0.1:443", &config)
    if err != nil {
        log.Fatalf("client: dial: %s", err)
    }
    defer conn.Close()
    log.Println("client: connected to: ", conn.RemoteAddr())
    state := conn.ConnectionState() 
    for _, v := range state.PeerCertificates {
        fmt.Println(x509.MarshalPKIXPublicKey(v.PublicKey))
        fmt.Println(v.Subject)
    }
    log.Println("client: handshake: ", state.HandshakeComplete)
    log.Println("client: mutual: ", state.NegotiatedProtocolIsMutual)
    message := "Hello\n"
    n, err := io.WriteString(conn, message)
    if err != nil {
        log.Fatalf("client: write: %s", err)
    }
    log.Printf("client: wrote %q (%d bytes)", message, n)
    reply := make([]byte, 256)
    n, err = conn.Read(reply)
    log.Printf("client: read %q (%d bytes)", string(reply[:n]), n)
    log.Print("client: exiting")
}

以上代码抛出此错误:

 "HTTP/1.1 400 No URI\r\nContent-Type: text/html;charset=iso-8859-1\r\nContent-Length: 49\r\nConnection: close\r\nServer: Jetty(9.4.19.v20190610)\r\n\r\n<h1>Bad Message 400</h1><pre>reason: No URI</pre>" (188 bytes)

我的问题是建立连接后如何向特定 URI 发送消息?即我想向 wss://localhost:443/server-demo.

发送消息

问题中的代码没有与服务器建立 WebSocket 连接。

要建立 WebSocket 连接,应用程序必须向 conn 写入 WebSocket 握手并接收握手响应。详情见RFC

大多数应用程序使用 websocket 包来处理所有这些细节。 gorilla/websocket 包是一个受欢迎的选择。

这段代码应该可以让您开始使用大猩猩:

cert, err := tls.LoadX509KeyPair("nifi-1.10.0-bin/nifi-1.10.0/extras/gen-certs/certs/admin.pem", "nifi-1.10.0-bin/nifi-1.10.0/extras/gen-certs/certs/admin-key.pem")
if err != nil {
    log.Fatalf("server: loadkeys: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
d := websocket.Dialer{
    TLSClientConfig: &config,
}
c, _, err := d.Dial("wss://localhost:443/server-demo", nil)
if err != nil {
    log.Fatal(err)
}

defer c.Close()
// Use `c` to send and receive messages