mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 10:10:08 -08:00
Make Message an interface with attached handling behavior.
This commit is contained in:
parent
d1f8c7657b
commit
fed72a7aa3
6 changed files with 190 additions and 96 deletions
|
|
@ -1,21 +1,13 @@
|
|||
package irc
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
command string
|
||||
args string
|
||||
client *Client
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
addr net.Addr
|
||||
send chan string
|
||||
recv chan string
|
||||
send chan<- string
|
||||
recv <-chan string
|
||||
username string
|
||||
realname string
|
||||
nick string
|
||||
|
|
@ -31,19 +23,12 @@ func NewClient(conn net.Conn) *Client {
|
|||
}
|
||||
|
||||
// Adapt `chan string` to a `chan Message`.
|
||||
func (c *Client) Communicate(server *Server) {
|
||||
go func() {
|
||||
for str := range c.recv {
|
||||
parts := strings.SplitN(str, " ", 2)
|
||||
server.Send(Message{parts[0], parts[1], c})
|
||||
func (c *Client) Communicate(server chan<- *ClientMessage) {
|
||||
for str := range c.recv {
|
||||
m := ParseMessage(str)
|
||||
if m != nil {
|
||||
server <- &ClientMessage{c, m}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Client) Send(lines ...string) {
|
||||
for _, line := range lines {
|
||||
log.Printf("C <- S: %s", line)
|
||||
c.send <- line
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue