-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_server.go
52 lines (43 loc) · 1.32 KB
/
table_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package hosting
import (
"context"
"github.com/gammazero/nexus/v3/client"
"github.com/gammazero/nexus/v3/router"
"github.com/gammazero/nexus/v3/wamp"
"github.com/sirupsen/logrus"
)
type TableServer struct {
client *client.Client
}
func NewTableServer(nxr router.Router, table string) (*TableServer, error) {
subscriber, err := client.ConnectLocal(nxr, client.Config{
Realm: table,
Logger: logrus.WithFields(logrus.Fields{"prefix": "wamp::client", "table": table}),
})
if err != nil {
return nil, err
}
err = subscriber.Register("say", func(ctx context.Context, invocation *wamp.Invocation) client.InvokeResult {
if len(invocation.Arguments) < 2 {
return client.InvokeResult{
Args: wamp.List{"Invalid Number of Arguments"},
Kwargs: wamp.Dict{"got": len(invocation.Arguments), "want at least": 2},
Err: "error.syntax",
}
}
if err := subscriber.Publish("state", wamp.Dict{}, invocation.Arguments, wamp.Dict{}); err != nil {
return client.InvokeResult{
Args: wamp.List{"Failed to publish state to other players"},
Err: "error.server",
}
}
return client.InvokeResult{Args: wamp.List{"ok"}}
}, wamp.Dict{"disclose_caller": true})
if err != nil {
return nil, err
}
return &TableServer{client: subscriber}, nil
}
func (t *TableServer) Close() error {
return t.client.Close()
}