mirror of
https://github.com/yudai/gotty.git
synced 2026-02-24 08:54:07 +01:00
Send data in base64 format
Raw data sometimes include invalid UTF-8 bytes and that brings errors to WebSocket clients. To avoid the errors, encode data into base64 before sending it.
This commit is contained in:
parent
83923b6f39
commit
4f75000256
3 changed files with 7 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -11,7 +12,6 @@ import (
|
|||
"unsafe"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/yudai/utf8reader"
|
||||
)
|
||||
|
||||
type clientContext struct {
|
||||
|
|
@ -93,16 +93,16 @@ func (context *clientContext) processSend() {
|
|||
}
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
utf8f := utf8reader.New(context.pty)
|
||||
|
||||
for {
|
||||
size, err := utf8f.Read(buf)
|
||||
size, err := context.pty.Read(buf)
|
||||
safeMessage := base64.StdEncoding.EncodeToString([]byte(buf[:size]))
|
||||
if err != nil {
|
||||
log.Printf("Command exited for: %s", context.request.RemoteAddr)
|
||||
return
|
||||
}
|
||||
|
||||
err = context.connection.WriteMessage(websocket.TextMessage, append([]byte{Output}, buf[:size]...))
|
||||
err = context.connection.WriteMessage(websocket.TextMessage, append([]byte{Output}, []byte(safeMessage)...))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue