mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 15:15:14 -06:00
Avoid panic on disconnect
This commit is contained in:
7
app.go
7
app.go
@@ -263,9 +263,12 @@ func (a *App) Start() {
|
|||||||
// entity listeners and event listeners
|
// entity listeners and event listeners
|
||||||
elChan := make(chan ws.ChanMsg)
|
elChan := make(chan ws.ChanMsg)
|
||||||
go ws.ListenWebsocket(a.conn, a.ctx, elChan)
|
go ws.ListenWebsocket(a.conn, a.ctx, elChan)
|
||||||
var msg ws.ChanMsg
|
|
||||||
for {
|
for {
|
||||||
msg = <-elChan
|
msg, ok := <-elChan
|
||||||
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
if a.entityListenersId == msg.Id {
|
if a.entityListenersId == msg.Id {
|
||||||
go callEntityListeners(a, msg.Raw)
|
go callEntityListeners(a, msg.Raw)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package websocket
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
@@ -20,7 +21,14 @@ type ChanMsg struct {
|
|||||||
|
|
||||||
func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg) {
|
func ListenWebsocket(conn *websocket.Conn, ctx context.Context, c chan ChanMsg) {
|
||||||
for {
|
for {
|
||||||
bytes, _ := ReadMessage(conn, ctx)
|
bytes, err := ReadMessage(conn, ctx)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Default().Println("Error reading from websocket:", err)
|
||||||
|
close(c)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
base := BaseMessage{}
|
base := BaseMessage{}
|
||||||
json.Unmarshal(bytes, &base)
|
json.Unmarshal(bytes, &base)
|
||||||
chanMsg := ChanMsg{
|
chanMsg := ChanMsg{
|
||||||
|
|||||||
Reference in New Issue
Block a user