-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
53 lines (45 loc) · 1.34 KB
/
message.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
// Copyright (c) 2016 IBM Corp. All rights reserved.
// Use of this source code is governed by the Apache License,
// Version 2.0, a copy of which can be found in the LICENSE file.
// Common response functions
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
)
type PlayerMessage struct {
Rtype string `json:"type,omitempty"`
Content map[string]string `json:"content,omitempty"`
Bookmark int `json:"bookmark,omitempty"`
}
var bookmark = 1
// Sends an event message to a player using the current websocket.
func SendMessageToPlayer(conn *websocket.Conn, mUser, uid string) (e error) {
var msg PlayerMessage
var j []byte
msg.Rtype = "event"
msg.Bookmark = bookmark
bookmark += 1
msg.Content = make(map[string]string)
msg.Content[uid] = mUser
j, e = json.MarshalIndent(msg, "", " ")
if e != nil {
return
}
e = SendMessage(conn, uid, j, MTPlayer)
return
}
// Sends a message with a JSON payload.
func SendMessage(conn *websocket.Conn, targetid string, j []byte, messageType string) (e error) {
locus := "SEND.MSG"
var m = fmt.Sprintf("%s,%s,%s", messageType, targetid, string(j))
e = conn.WriteMessage(ExpectedMessageType, []byte(m))
if config.debug {
checkpoint(locus, fmt.Sprintf("m=%s", m))
}
if e != nil {
checkpoint(locus, fmt.Sprintf("FAILED err=%s", e.Error()))
}
return
}