commit 4539d8571c98988bd48dcab6062366f9e8809750
parent f4a836e2d2cedcd9ad44eca8f4dba7d180ef19ef
Author: mdnrz <mehdeenoroozi@gmail.com>
Date: Tue, 8 Apr 2025 18:25:28 +0330
implement logout functionality
Diffstat:
| M | gotel.go | | | 26 | +++++++++++++++++++++----- |
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/gotel.go b/gotel.go
@@ -45,16 +45,25 @@ func addClient(conn net.Conn, Client_q chan Client) {
readBuf := make([]byte, 512)
for {
n, err := conn.Read(readBuf)
+ if n == 0 {
+ Client_q <- Client {
+ Request: msgQuit,
+ Conn: conn,
+ }
+ return;
+ }
+ if n > 0 {
+ Client_q <- Client {
+ Request: msgText,
+ Conn: conn,
+ Text: string(readBuf[:n]),
+ }
+ }
if err != nil {
log.Printf("Could not read message from client %s: %s\n", conn.RemoteAddr().String(), err)
conn.Close()
return
}
- Client_q <- Client {
- Request: msgText,
- Conn: conn,
- Text: string(readBuf[:n]),
- }
}
}
@@ -100,6 +109,13 @@ func server(Client_q chan Client) {
// TODO: implement rate limit for connection requests
log.Printf("Got login request from %s\n", keyString);
clientsOffline[keyString] = client;
+ case msgQuit:
+ author, ok := clientsOnline[keyString]
+ if ok {
+ log.Printf("%s logged out.\n", author.UserName);
+ author.Conn.Close();
+ delete(clientsOnline, keyString);
+ }
case msgText:
clientOffline, ok := clientsOffline[keyString];
if ok {