dongyue110702 2019-01-23 05:56
浏览 78

通过套接字连接发送/接收TUI数据

I have a TUI (text user interface) program that I would like to serve over a network connection. A sample from the login page (which doesn't do much right now), is posted below. I am using tui-go for my TUI library.

program.go

package main

import (
    "log"
    "github.com/marcusolsson/tui-go"
)

var logo string = "My Logo"

func main() {
    Login()
}

func Login() {
    inUser := tui.NewEntry()
    inUser.SetFocused(true)

    inPass := tui.NewEntry()
    inPass.SetEchoMode(tui.EchoModePassword)

    form := tui.NewGrid(0, 0)
    form.AppendRow(tui.NewLabel("Username"), tui.NewLabel("Password"))
    form.AppendRow(inUser, inPass)

    // TODO: create OnActivated function
    btnLogin := tui.NewButton("[Login]")

    // TODO: create OnActivated function
    btnRegister := tui.NewButton("[Register]")

    buttons := tui.NewHBox(
        tui.NewSpacer(),
        tui.NewPadder(1, 0, btnLogin),
        tui.NewPadder(1, 0, btnRegister),
    )

    window := tui.NewVBox(
        tui.NewPadder(1, 1, form),
        buttons,
    )
    window.SetBorder(true)

    root := tui.NewVBox(
        tui.NewSpacer(),
        window,
        tui.NewSpacer(),
    )

    tui.DefaultFocusChain.Set(inUser, inPass, btnLogin, btnRegister)

    ui, err := tui.New(root)
    if err != nil {
        log.Fatal(err)
    }

    ui.SetKeybinding("Esc", func() { ui.Quit() })

    if err := ui.Run(); err != nil {
        log.Fatal(err)
    }
}

I then have another program to execute the binary of program.go in a pty, and send it over a socket. Ideally, I would like the user to connect using a plethora of methods, such as telnet, ssh, or netcat. The problem is, when I send the pty over the socket, it does not behave the same way as it would when I'm simply running the binary of program.go from my shell. Below is a copy of my server program. I am using pty library to create the *os.File that gets sent over the socket.

server.go

package main

import (
    "net"
    "log"
    "io"
    "os/exec"
    "github.com/kr/pty"
)

func main() {
    ln, err := net.Listen("tcp", ":2323")
    if err != nil {
        log.Fatal(err)
    }
    defer ln.Close()
    for {
        conn, err := ln.Accept()
        if err != nil {
            log.Fatal(err)
        }
        go startProg(conn)
    }
}

func startProg(conn net.Conn) {
    defer conn.Close()

    term, err := pty.StartWithSize(
        exec.Command("./program"),
        &pty.Winsize{Cols: 100, Rows: 30},
    )
    if err != nil {
        log.Fatal(err)
    }
    defer term.Close()

    go io.Copy(term, conn)
    io.Copy(conn, term)
}

Is there anyway to have the TUI work as though it would normally work running from a shell? Also, how would I read in data that the user inputs into the TUI? Because when trying to read from both the connection, and the *os.File returned by pty, I can't seem to get any data at all.

Any help is much appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services