本文整理匯總了Golang中github.com/eclipse/che-lib/websocket.Conn類的典型用法代碼示例。如果您正苦於以下問題:Golang Conn類的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Conn類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: sendPtyOutputToConnection
// copy everything from the pty master to the websocket
// using base64 encoding for now due to limitations in term.js
func sendPtyOutputToConnection(conn *websocket.Conn, reader io.ReadCloser, finalizer *ReadWriteRoutingFinalizer) {
defer closeConn(conn, finalizer)
buf := make([]byte, 8192)
var buffer bytes.Buffer
for {
n, err := reader.Read(buf)
if err != nil {
if !isNormalPtyError(err) {
log.Printf("Failed to read from pty: %s", err)
}
return
}
i, err := normalizeBuffer(&buffer, buf, n)
if err != nil {
log.Printf("Cound't normalize byte buffer to UTF-8 sequence, due to an error: %s", err.Error())
return
}
if err = conn.WriteMessage(websocket.TextMessage, buffer.Bytes()); err != nil {
log.Printf("Failed to send websocket message: %s, due to occurred error %s", string(buffer.Bytes()), err.Error())
return
}
buffer.Reset()
if i < n {
buffer.Write(buf[i:n])
}
}
}
示例2: sendConnectionInputToPty
// read from the web socket, copying to the pty master
// messages are expected to be text and base64 encoded
func sendConnectionInputToPty(conn *websocket.Conn, reader io.ReadCloser, f *os.File, finalizer *ReadWriteRoutingFinalizer) {
defer closeReader(reader, f, finalizer)
for {
mt, payload, err := conn.ReadMessage()
if err != nil {
if !isNormalWsError(err) {
log.Printf("conn.ReadMessage failed: %s\n", err)
}
return
}
switch mt {
case websocket.BinaryMessage:
log.Printf("Ignoring binary message: %q\n", payload)
case websocket.TextMessage:
var msg WebSocketMessage
if err := json.Unmarshal(payload, &msg); err != nil {
log.Printf("Invalid message %s\n", err)
continue
}
if errMsg := handleMessage(msg, f); errMsg != nil {
log.Printf(errMsg.Error())
return
}
default:
log.Printf("Invalid websocket message type %d\n", mt)
return
}
}
}
示例3: transferAsJson
// transfers data from channel to physical connection,
// tries to transform data to json.
func transferAsJson(conn *websocket.Conn, c chan interface{}) {
for message := range c {
err := conn.WriteJSON(message)
if err != nil {
log.Printf("Couldn't write message to the channel. Message: %T, %v", message, message)
}
}
}
示例4: closeConn
func closeConn(conn *websocket.Conn, finalizer *ReadWriteRoutingFinalizer) {
defer finalizer.Unlock()
finalizer.Lock()
if !finalizer.writeDone {
conn.Close()
finalizer.writeDone = true
fmt.Println("Terminal writer closed.")
}
}
示例5: readMessages
// Reads the message from the websocket connection until error is received,
// returns the channel which should be used for reading such messages.
func readMessages(conn *websocket.Conn) chan *WsMessage {
messagesChan := make(chan *WsMessage)
go func() {
for {
_, bytes, err := conn.ReadMessage()
messagesChan <- &WsMessage{err: err, bytes: bytes}
if err != nil {
close(messagesChan)
break
}
}
}()
return messagesChan
}