成功连接 webscoket 后如何向特定 URI 发送消息?

我有一个在 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.


Qyouu
浏览 108回答 1
1回答

米脂

问题中的代码没有建立到服务器的 WebSocket 连接。要建立 WebSocket 连接,应用程序必须编写 WebSocket 握手conn并接收握手响应。有关详细信息,请参阅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 {&nbsp; &nbsp; log.Fatalf("server: loadkeys: %s", err)}config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}d := websocket.Dialer{&nbsp; &nbsp; TLSClientConfig: &config,}c, _, err := d.Dial("wss://localhost:443/server-demo", nil)if err != nil {&nbsp; &nbsp; log.Fatal(err)}defer c.Close()// Use `c` to send and receive messages
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go