Skip to content

Commit

Permalink
Send data in base64 format
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yudai committed Sep 3, 2015
1 parent 83923b6 commit 4f75000
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/client_context.go
@@ -1,6 +1,7 @@
package app

import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
Expand All @@ -11,7 +12,6 @@ import (
"unsafe"

"github.com/gorilla/websocket"
"github.com/yudai/utf8reader"
)

type clientContext struct {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions app/resource.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion resources/gotty.js
Expand Up @@ -53,7 +53,7 @@
data = event.data.slice(1);
switch(event.data[0]) {
case '0':
term.io.writeUTF16(data);
term.io.writeUTF8(window.atob(data));
break;
case '1':
// pong
Expand Down

0 comments on commit 4f75000

Please sign in to comment.