如何捕获像 ctrl+c 这样的 os 信号并通过 gorilla websocket 在 go 中发送它们
How to capture os signals like ctrl+c and send them through gorilla websocket in go
我是 go 和 websockets 的新手。我正在尝试逐个字符地输入并将它们写入 websocket。我什至想将 ctrl+c 作为输入并将其写入 websocket。
func (c *poc) writePump() {
var err error
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
var b = make([]byte, 1)
d := make(chan os.Signal, 1)
signal.Notify(d, os.Interrupt)
for {
os.Stdin.Read(b)
err = c.ws.WriteMessage(websocket.TextMessage, b)
if err != nil {
log.Printf("Failed to send UTF8 char: %s", err)
}
go func() {
for sig := range d {
// ????
}
}()
}
}
此代码正在捕获信号但不确定如何写入 websocket。
发送 CTRL-C 和
err = c.ws.WriteMessage(websocket.TextMessage, []byte{'[=10=]3'})
if err != nil {
// handle error
}
其他有用信息:
printf 提到了 UTF-8 字符,但不能保证该字节是有效的 UTF-8 字符。考虑改用 websocket.BinaryMessage。
使用互斥锁来防止并发写入 websocket 连接。
另一种方法是将信号带外发送到对等方并向远程进程发送信号。
我是 go 和 websockets 的新手。我正在尝试逐个字符地输入并将它们写入 websocket。我什至想将 ctrl+c 作为输入并将其写入 websocket。
func (c *poc) writePump() {
var err error
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
var b = make([]byte, 1)
d := make(chan os.Signal, 1)
signal.Notify(d, os.Interrupt)
for {
os.Stdin.Read(b)
err = c.ws.WriteMessage(websocket.TextMessage, b)
if err != nil {
log.Printf("Failed to send UTF8 char: %s", err)
}
go func() {
for sig := range d {
// ????
}
}()
}
}
此代码正在捕获信号但不确定如何写入 websocket。
发送 CTRL-C 和
err = c.ws.WriteMessage(websocket.TextMessage, []byte{'[=10=]3'})
if err != nil {
// handle error
}
其他有用信息:
printf 提到了 UTF-8 字符,但不能保证该字节是有效的 UTF-8 字符。考虑改用 websocket.BinaryMessage。
使用互斥锁来防止并发写入 websocket 连接。
另一种方法是将信号带外发送到对等方并向远程进程发送信号。