Skip to content

Commit

Permalink
feat: Implemented client input debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron3S committed Jun 4, 2024
1 parent 005289b commit 9e70dbc
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions pkg/httpd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package httpd
import (
"context"
"encoding/json"
"github.com/gliderlabs/ssh"
"io"
"sync"

"github.com/gliderlabs/ssh"
"time"

"github.com/jumpserver/koko/pkg/exchange"
"github.com/jumpserver/koko/pkg/logger"
Expand All @@ -20,6 +20,10 @@ type Client struct {
pty ssh.Pty

sync.Mutex

buffer []byte
bufferMutex sync.Mutex
timer *time.Timer
}

func (c *Client) WinCh() <-chan ssh.Window {
Expand All @@ -40,16 +44,36 @@ func (c *Client) Read(p []byte) (n int, err error) {
return c.UserRead.Read(p)
}

// 向客户端发送数据进行1毫秒的防抖处理
func (c *Client) Write(p []byte) (n int, err error) {
msg := Message{
Id: c.Conn.Uuid,
Type: TerminalBinary,
Raw: p,
c.bufferMutex.Lock()
defer c.bufferMutex.Unlock()

c.buffer = append(c.buffer, p...)

if c.timer == nil {
c.timer = time.AfterFunc(time.Millisecond, c.flushBuffer)
}
c.Conn.SendMessage(&msg)
return len(p), nil
}

func (c *Client) flushBuffer() {
c.bufferMutex.Lock()
defer c.bufferMutex.Unlock()

if len(c.buffer) > 0 {
msg := Message{
Id: c.Conn.Uuid,
Type: TerminalBinary,
Raw: c.buffer,
}
c.Conn.SendMessage(&msg)
c.buffer = nil
}
c.timer.Stop()
c.timer = nil
}

func (c *Client) Pty() ssh.Pty {
return c.pty
}
Expand Down

0 comments on commit 9e70dbc

Please sign in to comment.