Skip to content

Commit 0a2a0fd

Browse files
author
Jan Naahs
committed
handle closed channel
1 parent 27790cd commit 0a2a0fd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/wsclient.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (client *Client) Read() {
2929
handler(client, message.Data)
3030
}
3131
}
32+
client.socket.Close()
3233
}
3334

3435
func (client *Client) Write() {
@@ -37,6 +38,7 @@ func (client *Client) Write() {
3738
break
3839
}
3940
}
41+
client.socket.Close()
4042
}
4143

4244
func (client *Client) Close() {

src/wsroutes.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77
"github.com/hpcloud/tail"
88
)
99

10+
func IsClosed(ch <-chan Message) bool {
11+
select {
12+
case <-ch:
13+
return true
14+
default:
15+
}
16+
17+
return false
18+
}
19+
1020
func logSubscribe(client *Client, data interface{}) {
1121
go func() {
1222
logfile := filepath.Join(config.FactorioDir, "factorio-server-console.log")
@@ -17,7 +27,12 @@ func logSubscribe(client *Client, data interface{}) {
1727
}
1828

1929
for line := range t.Lines {
20-
client.send <- Message{"log update", line.Text}
30+
if !IsClosed(client.send) {
31+
client.send <- Message{"log update", line.Text}
32+
} else {
33+
log.Printf("Channel was closed")
34+
return
35+
}
2136
}
2237
}()
2338
}
@@ -35,7 +50,12 @@ func commandSend(client *Client, data interface{}) {
3550

3651
log.Printf("Command send to Factorio: %s, with rcon request id: %v", data, reqId)
3752

38-
client.send <- Message{"receive command", data}
53+
if !IsClosed(client.send) {
54+
client.send <- Message{"receive command", data}
55+
} else {
56+
log.Printf("Channel was closed")
57+
return
58+
}
3959
}()
4060
}
4161
}

0 commit comments

Comments
 (0)