major change of websocket dbmHandler structure
This commit is contained in:
34
dbm/publish.go
Normal file
34
dbm/publish.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/zuadi/tecamino-dbm/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Publish(ctx context.Context, eventType, path string, value any) error {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
for _, dp := range d.DB.QueryDatapoints(1, path) {
|
||||
for id, pub := range dp.Subscribtions {
|
||||
if client, ok := d.Clients[id]; !ok {
|
||||
delete(dp.Subscribtions, id)
|
||||
} else {
|
||||
if pub.OnChange {
|
||||
err := wsjson.Write(ctx, client.Conn, models.JsonResponse{
|
||||
Event: OnChange,
|
||||
Path: dp.Path,
|
||||
Value: value,
|
||||
})
|
||||
if err != nil {
|
||||
d.Log.Error("publish.Publish", err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user