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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型