每个人!我正在尝试让我的 go 代码与 openstack 串行控制台一起工作。它通过网络套接字公开。我有问题。我找到了 gorrilla websocket lib(很棒)并以此示例作为参考通过一些调整,现在我有这样的代码:package mainimport ("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' cmdlog.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("\n")) //just to force output to consolefor { 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 }}}
1 回答
倚天杖
TA贡献1828条经验 获得超3个赞
日志包将每条日志消息写在单独的行中。如果日志消息不以换行符结尾,那么日志包将添加一个。
这些额外的换行符会混淆输出。要修复输出,请将对的调用替换log.Printf("%s", message)
为不向输出添加换行符的函数。以下是一些选项:
将消息写入 stderr(与默认日志包配置相同的目的地):
os.Stderr.Write(message)
将消息写入标准输出(写入程序输出的更常规位置):
os.Stdout.Write(message)
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消