-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
166 lines (145 loc) · 4.03 KB
/
client.go
File metadata and controls
166 lines (145 loc) · 4.03 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"gopkg.in/qml.v0"
"os"
"strings"
)
var (
output chan string = make(chan string) //channel waitin on the user to type something
listUsers []string = make([]string, 0, 30) //users in the chat
myName string //name of the client
testing bool = false
)
type Control struct {
Root qml.Object
convstring string
userlist string
inputString string
}
//message sent out to the server
type Message struct {
Kind string //type of message ("CONNECT","PRIVATE","PUBLIC","DISCONNECT","ADD")
Username string //my username
Receiver string //if its a private message specifies the receiver of the message.
MSG string //message
Usernames []string
}
//start the connection, introduces the user to the chat and creates graphical interface.
func main() {
myName= os.Args[2]
//starting graphics
qml.Init(nil)
engine := qml.NewEngine()
ctrl := Control{convstring: ""}
ctrl.convstring = ""
context := engine.Context()
context.SetVar("ctrl", &ctrl)
component, err := engine.LoadFile("chat.qml")
if err != nil {
fmt.Println("no file to load for ui")
fmt.Println(err.Error())
os.Exit(0)
}
win := component.CreateWindow(nil)
ctrl.Root = win.Root()
service:= os.Args[1]+":12100"
tcpAddr, err := net.ResolveTCPAddr("tcp", service)
handleErr(err)
conn, err := net.DialTCP("tcp", nil, tcpAddr)
handleErr(err)
enc := json.NewEncoder(conn)
dec := json.NewDecoder(conn)
win.Show() //show window
ctrl.updateText("Hello "+myName+".\nFor private messages, type the message followed by * and the name of the receiver.\n To leave the conversation type disconnect")
introduceMyself(*enc)
go send(*enc)
go receive(*dec, &ctrl)
win.Wait()
}
//sends message to the server
func send(enc json.Encoder){
if testing {log.Println("send")}
msg:=new(Message)
for {
message:= <-output
whatever:=strings.Split(message,"*")
msg.Username=myName
if message=="disconnect"{
msg.Kind="DISCONNECT"
enc.Encode(msg)
break
} else if len(whatever)>1 {
msg.Kind="PRIVATE"
msg.Receiver=whatever[1]
msg.MSG=whatever[0]
} else {
msg.Kind="PUBLIC"
msg.MSG=whatever[0]
}
enc.Encode(msg)
}
os.Exit(1)
}
//receives message from server
func receive(dec json.Decoder, ctrl *Control){
if testing {log.Println("receive")}
msg:= new(Message)
for {
if err := dec.Decode(msg);err != nil {
fmt.Println("Something went wrong, closing connection")
panic(err)
return
}
if msg.Kind=="PRIVATE"{
ctrl.updateText(msg.Username+" wispers: "+msg.MSG)
}else if msg.Kind=="PUBLIC"{
ctrl.updateText(msg.Username+": "+msg.MSG)
}else if msg.Kind=="ADD" || msg.Kind=="DISCONNECT"{
ctrl.updateText(msg.MSG)
ctrl.updateList(msg.Usernames)
}else if msg.Kind=="SAMENAME"{
myName=msg.Username
ctrl.updateText(msg.MSG)
}
}
}
//introduces client to the chat
func introduceMyself(enc json.Encoder){
if testing {log.Println("introduceMyself")}
msg:=new(Message)
msg.Kind="CONNECT"
msg.Username=myName
enc.Encode(msg)
}
func handleErr(err error) {
if err != nil {
log.Fatal(err)
}
}
//Graphics methods
func (ctrl *Control) TextEntered(text qml.Object) {
//this method is called whenever a return key is typed in the text entry field. The qml object calls this function
ctrl.inputString = text.String("text") //the ctrl's inputString field holds the message
//you will want to send it to the server
//but for now just send it back to the conv field
//ctrl.updateText(ctrl.inputString)
output <- ctrl.inputString
}
func (ctrl *Control) updateText(toAdd string) {
//call this method whenever you want to add text to the qml object's conv field
ctrl.convstring = ctrl.convstring + toAdd + "\n" //also keep track of everything in that field
ctrl.Root.ObjectByName("conv").Set("text", ctrl.convstring)
qml.Changed(ctrl, &ctrl.convstring)
}
func (ctrl *Control) updateList(list []string) {
ctrl.userlist = ""
for _, user := range list {
ctrl.userlist += user + "\n"
}
ctrl.Root.ObjectByName("userlist").Set("text", ctrl.userlist)
qml.Changed(ctrl, &ctrl.userlist)
}