Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP example : Echo - but what would the right practice be if you wanted to change to a chat server ? #54

Closed
spexocta opened this issue Oct 12, 2020 · 0 comments

Comments

@spexocta
Copy link

The example provided is an echo server - but what if i wanted to change it into a 'chat server' - that would send data from one client to all other connected clients than the one that sent the data ?

Am unsure how it should be structured correctly :


		// Convert the IP:socket for populating TCPAddr value.
		parts := bytes.Split([]byte(c.ipAddress), []byte(":"))
		ipAddress := string(parts[0])
		port, _ := strconv.Atoi(string(parts[1]))

		// Create the request.
		r := Request{
			TCP: c.t,
			TCPAddr: &net.TCPAddr{
				IP:   net.ParseIP(ipAddress),
				Port: port,
				Zone: c.t.tcpAddr.Zone,
			},
			IsIPv6:  c.isIPv6,
			ReadAt:  c.lastAct,
			Context: context.Background(),
			Data:    data,
			Length:  length,
		}

		// Process the request on this goroutine that is
		// handling the socket connection.
		c.t.ReqHandler.Process(&r)

Process() is where it calls the routine that in the end will send to that specific client - but if i instead looped through all clients that was not the sending client calling process on each of them something im not sure if that is the correct way ?

any ideas on how i would need to that the correct way ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant