diff options
author | EuAndreh <eu@euandre.org> | 2024-06-24 16:50:41 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-06-24 16:50:41 -0300 |
commit | 90aecdeb14078457f59594c966b3958470f411d4 (patch) | |
tree | d2bec8897eeb77dbd6a2ac5749f70ff4141ee309 /src | |
parent | rm -rf src/static/ (diff) | |
download | papod-90aecdeb14078457f59594c966b3958470f411d4.tar.gz papod-90aecdeb14078457f59594c966b3958470f411d4.tar.xz |
src/lib.go: Add simplistic scaffold of the connection WriteLoop()
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.go | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -7,6 +7,7 @@ import ( "errors" "flag" "fmt" + "io" "io/ioutil" "log/slog" "net" @@ -31,9 +32,10 @@ var ( Colour string ) -var EmitActiveConnection = g.MakeGauge("active-connections") -var EmitNicksInChannel = g.MakeGauge("nicks-in-channel") -var EmitReceivedMessage = g.MakeCounter("received-message") +var EmitActiveConnection = g.MakeGauge("active-connections") +var EmitNicksInChannel = g.MakeGauge("nicks-in-channel") +var EmitReceivedMessage = g.MakeCounter("received-message") +var EmitWriteToClientError = g.MakeCounter("write-to-client") const pingFrequency = time.Duration(30) * time.Second const pongMaxLatency = time.Duration(5) * time.Second @@ -50,6 +52,7 @@ type Context struct { type Connection struct { conn net.Conn + replyChan chan string // id *UUID id string isAuthenticated bool @@ -281,7 +284,21 @@ func ReadLoop(ctx *Context, connection *Connection) { } func WriteLoop(ctx *Context, connection *Connection) { - // fmt.Println("WriteLoop") + for message := range connection.replyChan { + _, err := io.WriteString(connection.conn, message) + if err != nil { + g.Error( + "Failed to send data to user", + "user-reply-error", + "err", err, + ) + EmitWriteToClientError() + continue + } + } + + EmitActiveConnection.Dec() + connection.conn.Close() } func PingLoop(ctx *Context, connection *Connection) { |