forked from zhravan/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_chat_app.go
More file actions
54 lines (44 loc) · 1.08 KB
/
Copy pathsimple_chat_app.go
File metadata and controls
54 lines (44 loc) · 1.08 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
package simple_chat_app
import (
"net"
"sync"
)
// TODO:
// - Implement a basic TCP chat server:
// - Start: listen on a TCP port and accept connections.
// - Track clients with unique IDs and default names (e.g., Guest1).
// - Broadcast: send messages from one client to all others.
// - Ensure concurrent access to client map is synchronized.
type Client struct {
conn net.Conn
server *Server
id int
name string
}
type Server struct {
clients map[int]*Client
mu sync.Mutex
nextID int
listener net.Listener
}
func NewServer() *Server {
// TODO: initialize server state
return &Server{}
}
func (s *Server) Start(port string) error {
// TODO: start listening and accept connections
return nil
}
func (s *Server) Stop() error {
// TODO: stop the server/listener
return nil
}
func (s *Server) acceptConnections() {
// TODO: accept and register clients
}
func (c *Client) handleConnection() {
// TODO: read messages from the client and broadcast
}
func (s *Server) Broadcast(sender *Client, message string) {
// TODO: broadcast message to other clients
}