add notes and scaffolding for publish/subscribe logic

This commit is contained in:
Iris Lightshard 2023-02-11 10:30:26 -07:00
parent 4e3ef2d27d
commit cd14def1f8
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
3 changed files with 45 additions and 20 deletions

View file

@ -86,18 +86,11 @@ func (self *GameTableServer) subscribe(r *http.Request, c *websocket.Conn) error
}, },
} }
tableName, tblNameErr := cookie.GetToken("tableName", r) tableKey := models.TableKey{}
tablePasscode, tblPassErr := cookie.GetToken("tablePasscode", r) err := json.NewDecoder(r.Body).Decode(&tableKey)
if err != nil {
if tblNameErr != nil { fmt.Println(err.Error())
return tblNameErr return
} else if tblPassErr != nil {
return tblPassErr
}
tableKey := models.TableKey{
Name: tableName,
Passcode: tablePasscode,
} }
if !self.dbAdapter.CheckTable(tableKey) { if !self.dbAdapter.CheckTable(tableKey) {
@ -148,23 +141,38 @@ func (self *GameTableServer) publish(msg []byte) {
defer self.subscribersLock.Unlock() defer self.subscribersLock.Unlock()
// decode message and store in DB // decode message and store in DB
tableMsg := models.TableMessage{}
err := json.NewDecoder(msg).Decode(&tableMsg)
if err != nil {
fmt.Println(err.Error())
return
}
self.writeToDB(tableMsg)
self.publishLimiter.Wait(context.Background()) self.publishLimiter.Wait(context.Background())
for s := range self.subscribers { for s, k := range self.subscribers {
select { if k == tableMsg.tableKey {
case s.msgs <- msg: select {
default: case s.msgs <- msg:
go s.closeSlow() default:
go s.closeSlow()
}
} }
} }
} }
func (self *GameTableServer) getCurrentState(tableKey models.TableKey) []byte { func (self *GameTableServer) getCurrentState(tableKey models.TableKey) ([]byte, error) {
// get diceroll log, map, and token state // get diceroll log, map, and token state
// build into a []byte message // build into a []byte message
return make([]byte, 1) return make([]byte, 1), nil
}
func (self *GameTableServer) writeToDB(tableMsg models.TableMessage) error {
return nil
} }
func (self *GameTableServer) addSubscriber(s *Subscriber, k models.TableKey) { func (self *GameTableServer) addSubscriber(s *Subscriber, k models.TableKey) {

View file

@ -36,6 +36,7 @@ type Table struct {
} }
type TableMessage struct { type TableMessage struct {
Key TableKey `json:"key"`
Roll DiceRoll `json:"roll"` Roll DiceRoll `json:"roll"`
Token Token `json:"token"` Token Token `json:"token"`
MapImg string `json:"mapImg"` MapImg string `json:"mapImg"`

View file

@ -28,3 +28,19 @@ admin routes:
- Get /storage/ { - Get /storage/ {
static storage tree static storage tree
} }
subscribe:
subscribe (tableKey) => {
subscriber[s] = tablekey;
return getCurrentState(tableKey)
}
publish:
publish (updateData) => {
writeToDB(updateData)
for s, k := range subscribers
if k == updateData.tableKey {
s.msgs <- updateData
}
}