major change of websocket dbmHandler structure
This commit is contained in:
46
dbm/subscribe.go
Normal file
46
dbm/subscribe.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/zuadi/tecamino-dbm/models"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Subscribe(ctx context.Context, sub *models.Subscribe, id string) {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
if sub.Driver != nil {
|
||||
if dp.Drivers == nil {
|
||||
continue
|
||||
} else if _, ok := dp.Drivers[*sub.Driver]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
dp.AddSubscribtion(id, sub)
|
||||
|
||||
err := wsjson.Write(ctx, d.Clients[id].Conn, models.JsonResponse{
|
||||
Event: OnCreate,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
})
|
||||
if err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) Unsubscribe(ctx context.Context, sub *models.Subscribe, id string) error {
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
if _, ok := dp.Subscribtions[id]; !ok {
|
||||
continue
|
||||
}
|
||||
dp.RemoveSubscribtion(id)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user