mirror of
https://github.com/yudai/gotty.git
synced 2026-01-04 08:38:50 +01:00
Refine API of webtty package
This commit is contained in:
parent
d1ec7125cf
commit
807bcc25a4
10 changed files with 82 additions and 97 deletions
|
|
@ -148,29 +148,17 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn) e
|
|||
if server.options.EnableReconnect {
|
||||
opts = append(opts, webtty.WithReconnect(server.options.ReconnectTime))
|
||||
}
|
||||
if server.options.Width > 0 || server.options.Height > 0 {
|
||||
width, height, err := slave.GetTerminalSize()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get default terminal size")
|
||||
}
|
||||
if server.options.Width > 0 {
|
||||
width = server.options.Width
|
||||
}
|
||||
if server.options.Height > 0 {
|
||||
height = server.options.Height
|
||||
}
|
||||
err = slave.ResizeTerminal(width, height)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to resize terminal")
|
||||
}
|
||||
|
||||
opts = append(opts, webtty.WithFixedSize(server.options.Width, server.options.Height))
|
||||
if server.options.Width > 0 {
|
||||
opts = append(opts, webtty.WithFixedColumns(server.options.Width))
|
||||
}
|
||||
if server.options.Height > 0 {
|
||||
opts = append(opts, webtty.WithFixedRows(server.options.Height))
|
||||
}
|
||||
if server.options.Preferences != nil {
|
||||
opts = append(opts, webtty.WithMasterPreferences(server.options.Preferences))
|
||||
}
|
||||
|
||||
tty, err := webtty.New(conn, slave, opts...)
|
||||
tty, err := webtty.New(&wsWrapper{conn}, slave, opts...)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to create webtty")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
type Slave interface {
|
||||
webtty.Slave
|
||||
|
||||
GetTerminalSize() (width int, height int, err error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Factory interface {
|
||||
|
|
|
|||
33
server/ws_wrapper.go
Normal file
33
server/ws_wrapper.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type wsWrapper struct {
|
||||
*websocket.Conn
|
||||
}
|
||||
|
||||
func (wsw *wsWrapper) Write(p []byte) (n int, err error) {
|
||||
writer, err := wsw.Conn.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer writer.Close()
|
||||
return writer.Write(p)
|
||||
}
|
||||
|
||||
func (wsw *wsWrapper) Read(p []byte) (n int, err error) {
|
||||
for {
|
||||
msgType, reader, err := wsw.Conn.NextReader()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if msgType != websocket.TextMessage {
|
||||
continue
|
||||
}
|
||||
|
||||
return reader.Read(p)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue