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)
tablePasscode, tblPassErr := cookie.GetToken("tablePasscode", r)
if tblNameErr != nil {
return tblNameErr
} else if tblPassErr != nil {
return tblPassErr
}
tableKey := models.TableKey{
Name: tableName,
Passcode: tablePasscode,
tableKey := models.TableKey{}
err := json.NewDecoder(r.Body).Decode(&tableKey)
if err != nil {
fmt.Println(err.Error())
return
}
if !self.dbAdapter.CheckTable(tableKey) {
@ -148,10 +141,19 @@ func (self *GameTableServer) publish(msg []byte) {
defer self.subscribersLock.Unlock()
// 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())
for s := range self.subscribers {
for s, k := range self.subscribers {
if k == tableMsg.tableKey {
select {
case s.msgs <- msg:
default:
@ -159,12 +161,18 @@ func (self *GameTableServer) publish(msg []byte) {
}
}
}
}
func (self *GameTableServer) getCurrentState(tableKey models.TableKey) []byte {
func (self *GameTableServer) getCurrentState(tableKey models.TableKey) ([]byte, error) {
// get diceroll log, map, and token state
// 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) {

View file

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

View file

@ -28,3 +28,19 @@ admin routes:
- Get /storage/ {
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
}
}