dongmou3615
dongmou3615
2019-02-04 17:44

无法弄清楚如何在二进制Web套接字中使用缓冲区

已采纳

everyone! I'm trying to get my go code work with openstack serial console. It`s exposed via web socket. And i have problems with it.

I found gorrilla websocket lib (which is great) and took this example as a reference

With a few tweaks, now i have a code like this:

package main
import (
"log"
"net/url"
"os"
"os/signal"
"time"
"net/http"

"github.com/gorilla/websocket"
)


func main() {
DialSettings := &websocket.Dialer {
    Proxy:            http.ProxyFromEnvironment,
    HandshakeTimeout: 45 * time.Second,
    Subprotocols: []string{"binary",},
    ReadBufferSize: 4096,
    WriteBufferSize: 4096,
}
log.SetFlags(0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

u, _ := url.Parse("ws://172.17.0.64:6083/?token=d1763f2b-3466-424c-aece-6aeea2a733d5") //websocket url  as it outputs from 'nova get-serial-console test' cmd
log.Printf("connecting to %s", u.String())

c, _, err := DialSettings.Dial(u.String(), nil)
if err != nil {
    log.Fatal("dial:", err)
}
defer c.Close()

done := make(chan struct{})

go func() {
    defer close(done)
    for {
        _, message, err := c.ReadMessage()
        if err != nil {
            log.Println("read:", err)
            return
        }
        log.Printf("%s", message)
    }
}()
c.WriteMessage(websocket.TextMessage, []byte("
")) //just to force output to console

for {
    select {
    case <-done:
        return
    case <-interrupt:
        log.Println("interrupt")

        // Cleanly close the connection by sending a close message and then
        // waiting (with timeout) for the server to close the connection.
        err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
        if err != nil {
            log.Println("write close:", err)
            return
        }
        select {
        case <-done:
        case <-time.After(time.Second):
        }
        return
    }
}

}

And i get output like this:

connecting to ws://172.17.0.64:6083/?token=d1763f2b-3466-424c-aece-6aeea2a733d5


CentOS Linux 7
(C
ore)
K
erne
l
3.10.0-862.el7.x86_64
 o
n an
 x
86_64

centos
-test login:

Total mess...

I think it's because i recieve just a chunks of bytes with no way to delimit them. I need some buffer to store them and when do something like bufio.ReadLine. But i'm not most experienced go programmer, and i run out of ideas how to do this. At the end i just need strings to work with.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drwkqwa82140 drwkqwa82140 2年前

    The log package writes each log message on a separate line. If the log message does not end with a newline, then the log package will add one.

    These extra newlines are garbling the output. To fix the output, replace the call to log.Printf("%s", message) with a function that does not add newlines to the output. Here are some options:

    Write the message to stderr (same destination as default log package config):

    os.Stderr.Write(message)
    

    Write the message to stdout (a more conventional location to write program output):

    os.Stdout.Write(message)
    
    点赞 评论 复制链接分享

相关推荐