-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.go
More file actions
50 lines (43 loc) · 1.17 KB
/
messages.go
File metadata and controls
50 lines (43 loc) · 1.17 KB
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
// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package collider
import (
"encoding/json"
"io"
)
// WebSocket message from the client.
type wsClientMsg struct {
Cmd string `json:"cmd"`
RoomID string `json:"roomid"`
ClientID string `json:"clientid"`
Msg string `json:"msg"`
}
// wsServerMsg is a message sent to a client on behalf of another client.
type wsServerMsg struct {
Msg string `json:"msg"`
Error string `json:"error"`
}
// sendServerMsg sends a wsServerMsg composed from |msg| to the connection.
func sendServerMsg(w io.Writer, msg string) error {
m := wsServerMsg{
Msg: msg,
}
return send(w, m)
}
// sendServerErr sends a wsServerMsg composed from |errMsg| to the connection.
func sendServerErr(w io.Writer, errMsg string) error {
m := wsServerMsg{
Error: errMsg,
}
return send(w, m)
}
// send writes a generic object as JSON to the writer.
func send(w io.Writer, data interface{}) error {
enc := json.NewEncoder(w)
if err := enc.Encode(data); err != nil {
return err
}
return nil
}