dongmou3615 2019-02-04 17:44
浏览 30
已采纳

无法弄清楚如何在二进制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 2019-02-04 19:52
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么