-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (77 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/gorilla/websocket"
)
var scanner = bufio.NewScanner(os.Stdin)
func readMessage(conn *websocket.Conn) {
for {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Read error: ", err)
return
}
fmt.Println(string(msg))
}
}
func writeMessage(conn *websocket.Conn) {
for {
// trying to stop the message from echoing locally...
// fd := int(os.Stdin.Fd())
// oldState, err := terminal.MakeRaw(fd)
// if err != nil {
// fmt.Println("Error setting raw terminal:", err)
// return
// }
// var input []byte
// for {
// var b [1]byte
// _, err := os.Stdin.Read(b[:])
// if err != nil {
// fmt.Println("Error reading input:", err)
// return
// }
// // Break loop if Enter key is pressed
// if b[0] == '\n' {
// terminal.Restore(fd, oldState)
// break
// }
// // Append to input buffer
// input = append(input, b[0])
// fmt.Print(b)
// }
// fmt.Print("\033[2K\033[1G") // Clear current line
scanner.Scan()
input := scanner.Text()
if len(input) <= 0 {
continue
}
err := conn.WriteMessage(websocket.TextMessage, []byte(input))
if err != nil {
log.Println("Write error: ", err)
return
}
}
}
func main() {
fmt.Print("Enter room name: ")
scanner.Scan()
roomName := scanner.Text()
//add self as sender client
url := "ws://localhost:5005/ws/" + string(roomName)
log.Printf("Connecting to %s", url)
conn, resp, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
if err == websocket.ErrBadHandshake {
log.Printf("handshake failed with status %d", resp.StatusCode)
return
}
log.Println("[DIAL]", err)
return
}
go readMessage(conn)
writeMessage(conn)
}