-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.go
63 lines (53 loc) · 1.6 KB
/
Client.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
53
54
55
56
57
58
59
60
61
62
63
package eternal
import (
"encoding/json"
"fmt"
"log"
"github.com/CharlesHolbrow/synk"
)
// Client implements custom handlers
type Client struct {
}
// OnConnect is called when client connects via WebSocket
func (cc Client) OnConnect(client synk.Client) {
log.Println("Custom Client Connected:", client.ID())
}
// OnMessage is called when the client sends a message
func (cc Client) OnMessage(client synk.Client, method string, data []byte) {
log.Println("Custom Client Message:", method)
switch method {
case "moveCell":
event := &MoveCellEvent{}
if err := json.Unmarshal(data, event); err == nil {
log.Println("MoveCellEvent:", event)
cc.handleMoveCellEvent(client, *event)
} else {
fmt.Println("CLient requested bad moveCell event:", err)
}
}
}
// OnSubscribe is called with the client changes their subscription
func (cc Client) OnSubscribe(client synk.Client, subKeys []string, objs []synk.Object) {
log.Printf("Custom Client: Subscription add(%d) objs(%d)", len(subKeys), len(objs))
}
func (cc Client) handleMoveCellEvent(client synk.Client, e MoveCellEvent) {
if json, err := json.Marshal(e); err == nil {
client.Publish("piano", json)
} else {
log.Println("failed to re-marshall MoveCellEvent")
}
}
/***************************************************************
/
/ Messages that may be sent from clients
/
***************************************************************/
// MoveCellEvent is send by the client as a request
type MoveCellEvent struct {
ID string
X int
Y int
}
func (e MoveCellEvent) String() string {
return fmt.Sprintf("Move %s to (%d, %d)", e.ID, e.X, e.Y)
}