Skip to content

Commit

Permalink
filter null message
Browse files Browse the repository at this point in the history
  • Loading branch information
lmortezal committed Feb 21, 2024
1 parent 30d56cd commit 134a715
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions server/server-side.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import (
var (
messages = make(chan string)
clients = make(map[Client]bool)
// messages2 = make(chan allmessages)
)

// TODO // create a struct for the messages to filter dublicate message
// type allmessages struct {
// Conn net.Conn
// Message string
// }

type Client struct {
Conn net.Conn
Name string
Expand Down Expand Up @@ -58,6 +65,8 @@ func Startlistening(domain string, port int) {

}


// just Boradcast the message to all clients
func Boradcast(){
for msg := range messages {
for cli := range clients {
Expand All @@ -68,8 +77,6 @@ func Boradcast(){

// Handles incoming requests.
func handleRequest(conn net.Conn) {
// print data in conn ( data is a buffer)
// print human readable data
input := bufio.NewScanner(conn)
fmt.Println("New connection from:", conn.RemoteAddr())
var name string
Expand All @@ -80,34 +87,14 @@ func handleRequest(conn net.Conn) {

cli := Client{conn, name}
clients[cli] = true
// Make a buffer to hold incoming data.
// buf := make([]byte, 1024)
// for input.Scan(){
// // Read the incoming connection into the buffer.
// if clients[Client{conn, ""}]{
// fmt.Println("New client")
// // _, err := conn.Read(buf)
// // if err != nil {
// // log.Println("Error reading:", err.Error())
// // break
// // }
// fmt.Printf("Name received: %s", input.Text())
// for cli := range clients {
// if cli.Conn == conn{
// cli.Name = input.Text()
// }
// }
// clients[Client{conn, ""}] = false
// continue
// }
// }



for input.Scan(){
// Convert the buffer to a string and print it.
messages <- input.Text()
if input.Text() == ""{
continue
}
messages <- name + " say : " + input.Text()
fmt.Println("Received from ", name, ":", input.Text())
// Send the received string back to the client.
//conn.Write([]byte("Message received: " + reqStr))
}
}

0 comments on commit 134a715

Please sign in to comment.