2015-10-26 11:22:06 -07:00
|
|
|
package server // import "bitbucket.org/stendec/frankerfacez/socketserver/internal/server"
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-10-26 10:06:45 -07:00
|
|
|
"errors"
|
2015-10-24 19:59:34 -07:00
|
|
|
"fmt"
|
2015-10-28 15:19:22 -07:00
|
|
|
"github.com/gorilla/websocket"
|
2015-10-27 21:21:06 -07:00
|
|
|
"io"
|
2015-10-28 18:12:20 -07:00
|
|
|
"io/ioutil"
|
2015-10-25 12:40:07 -07:00
|
|
|
"log"
|
2015-10-26 10:06:45 -07:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-10-28 15:19:22 -07:00
|
|
|
"time"
|
2015-10-24 19:59:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const MAX_PACKET_SIZE = 1024
|
|
|
|
|
|
|
|
// A command is how the client refers to a function on the server. It's just a string.
|
|
|
|
type Command string
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
// A function that is called to respond to a Command.
|
2015-10-24 21:33:55 -07:00
|
|
|
type CommandHandler func(*websocket.Conn, *ClientInfo, ClientMessage) (ClientMessage, error)
|
|
|
|
|
2015-10-24 21:42:16 -07:00
|
|
|
var CommandHandlers = map[Command]CommandHandler{
|
2015-10-24 21:33:55 -07:00
|
|
|
HelloCommand: HandleHello,
|
2015-10-26 10:06:45 -07:00
|
|
|
"setuser": HandleSetUser,
|
2015-10-26 14:55:20 -07:00
|
|
|
"ready": HandleReady,
|
2015-10-25 00:44:25 -07:00
|
|
|
|
2015-10-26 14:55:20 -07:00
|
|
|
"sub": HandleSub,
|
|
|
|
"unsub": HandleUnsub,
|
2015-10-25 00:44:25 -07:00
|
|
|
|
2015-10-26 10:06:45 -07:00
|
|
|
"track_follow": HandleTrackFollow,
|
2015-10-24 21:33:55 -07:00
|
|
|
"emoticon_uses": HandleEmoticonUses,
|
2015-10-26 10:06:45 -07:00
|
|
|
"survey": HandleSurvey,
|
2015-10-25 00:44:25 -07:00
|
|
|
|
2015-10-26 10:06:45 -07:00
|
|
|
"twitch_emote": HandleRemoteCommand,
|
2015-11-01 13:17:35 -08:00
|
|
|
"get_link": HandleBunchedRemotecommand,
|
|
|
|
"get_display_name": HandleBunchedRemotecommand,
|
2015-10-25 00:44:25 -07:00
|
|
|
"update_follow_buttons": HandleRemoteCommand,
|
2015-10-26 10:06:45 -07:00
|
|
|
"chat_history": HandleRemoteCommand,
|
2015-10-24 21:33:55 -07:00
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
// Sent by the server in ClientMessage.Command to indicate success.
|
2015-11-01 00:30:49 -07:00
|
|
|
const SuccessCommand Command = "ok"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
// Sent by the server in ClientMessage.Command to indicate failure.
|
|
|
|
const ErrorCommand Command = "error"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-10-24 22:38:04 -07:00
|
|
|
// This must be the first command sent by the client once the connection is established.
|
2015-10-24 21:33:55 -07:00
|
|
|
const HelloCommand Command = "hello"
|
2015-10-26 10:06:45 -07:00
|
|
|
|
2015-10-24 22:38:04 -07:00
|
|
|
// A handler returning a ClientMessage with this Command will prevent replying to the client.
|
|
|
|
// It signals that the work has been handed off to a background goroutine.
|
|
|
|
const AsyncResponseCommand Command = "_async"
|
2015-10-24 19:59:34 -07:00
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
var SocketUpgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return r.Header.Get("Origin") == "http://www.twitch.tv"
|
|
|
|
},
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errors that get returned to the client.
|
|
|
|
var ProtocolError error = errors.New("FFZ Socket protocol error.")
|
2015-10-25 20:17:17 -07:00
|
|
|
var ProtocolErrorNegativeID error = errors.New("FFZ Socket protocol error: negative or zero message ID.")
|
2015-10-24 19:59:34 -07:00
|
|
|
var ExpectedSingleString = errors.New("Error: Expected single string as arguments.")
|
2015-10-25 00:44:25 -07:00
|
|
|
var ExpectedSingleInt = errors.New("Error: Expected single integer as arguments.")
|
2015-10-24 19:59:34 -07:00
|
|
|
var ExpectedTwoStrings = errors.New("Error: Expected array of string, string as arguments.")
|
|
|
|
var ExpectedStringAndInt = errors.New("Error: Expected array of string, int as arguments.")
|
2015-10-25 00:44:25 -07:00
|
|
|
var ExpectedStringAndBool = errors.New("Error: Expected array of string, bool as arguments.")
|
2015-10-24 19:59:34 -07:00
|
|
|
var ExpectedStringAndIntGotFloat = errors.New("Error: Second argument was a float, expected an integer.")
|
|
|
|
|
2015-10-26 22:16:03 -07:00
|
|
|
var gconfig *ConfigFile
|
2015-10-25 03:21:50 -07:00
|
|
|
|
2015-10-28 18:12:20 -07:00
|
|
|
var BannerHTML []byte
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
// Set up a websocket listener and register it on /.
|
|
|
|
// (Uses http.DefaultServeMux .)
|
|
|
|
func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
2015-10-25 03:21:50 -07:00
|
|
|
gconfig = config
|
|
|
|
|
|
|
|
SetupBackend(config)
|
|
|
|
|
2015-10-25 20:17:17 -07:00
|
|
|
if serveMux == nil {
|
|
|
|
serveMux = http.DefaultServeMux
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-10-28 18:12:20 -07:00
|
|
|
bannerBytes, err := ioutil.ReadFile("index.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not open index.html", err)
|
|
|
|
}
|
|
|
|
BannerHTML = bannerBytes
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
serveMux.HandleFunc("/", ServeWebsocketOrCatbag)
|
2015-10-26 10:24:28 -07:00
|
|
|
serveMux.HandleFunc("/pub_msg", HBackendPublishRequest)
|
2015-10-26 18:00:29 -07:00
|
|
|
serveMux.HandleFunc("/dump_backlog", HBackendDumpBacklog)
|
2015-10-26 10:24:28 -07:00
|
|
|
serveMux.HandleFunc("/update_and_pub", HBackendUpdateAndPublish)
|
2015-10-28 15:19:22 -07:00
|
|
|
|
|
|
|
go deadChannelReaper()
|
|
|
|
go backlogJanitor()
|
|
|
|
go sendAggregateData()
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
func ServeWebsocketOrCatbag(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Connection") == "Upgrade" {
|
|
|
|
conn, err := SocketUpgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "error: %v", err)
|
2015-10-26 18:00:29 -07:00
|
|
|
return
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
HandleSocketConnection(conn)
|
|
|
|
|
|
|
|
return
|
|
|
|
} else {
|
2015-10-28 18:12:20 -07:00
|
|
|
w.Write(BannerHTML)
|
2015-10-26 18:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
var CloseGotBinaryMessage = websocket.CloseError{Code: websocket.CloseUnsupportedData, Text: "got binary packet"}
|
|
|
|
var CloseGotMessageId0 = websocket.CloseError{Code: websocket.ClosePolicyViolation, Text: "got messageid 0"}
|
2015-10-28 22:59:27 -07:00
|
|
|
var CloseTimedOut = websocket.CloseError{Code: websocket.CloseNoStatusReceived, Text: "no ping replies for 5 minutes"}
|
2015-10-28 17:31:44 -07:00
|
|
|
var CloseFirstMessageNotHello = websocket.CloseError{
|
|
|
|
Text: "Error - the first message sent must be a 'hello'",
|
|
|
|
Code: websocket.ClosePolicyViolation,
|
|
|
|
}
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
// Handle a new websocket connection from a FFZ client.
|
|
|
|
// This runs in a goroutine started by net/http.
|
|
|
|
func HandleSocketConnection(conn *websocket.Conn) {
|
|
|
|
// websocket.Conn is a ReadWriteCloser
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
log.Println("Got socket connection from", conn.RemoteAddr())
|
2015-10-27 21:21:06 -07:00
|
|
|
|
2015-10-24 21:33:55 -07:00
|
|
|
var _closer sync.Once
|
|
|
|
closer := func() {
|
|
|
|
_closer.Do(func() {
|
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// Close the connection when we're done.
|
|
|
|
defer closer()
|
2015-10-24 20:40:49 -07:00
|
|
|
|
|
|
|
_clientChan := make(chan ClientMessage)
|
|
|
|
_serverMessageChan := make(chan ClientMessage)
|
|
|
|
_errorChan := make(chan error)
|
2015-10-29 01:30:25 -07:00
|
|
|
stoppedChan := make(chan struct{})
|
2015-10-24 20:40:49 -07:00
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
var client ClientInfo
|
|
|
|
client.MessageChannel = _serverMessageChan
|
2015-11-01 13:17:35 -08:00
|
|
|
client.RemoteAddr = conn.RemoteAddr()
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// Launch receiver goroutine
|
2015-10-29 01:30:25 -07:00
|
|
|
go func(errorChan chan<- error, clientChan chan<- ClientMessage, stoppedChan <-chan struct{}) {
|
2015-10-24 20:40:49 -07:00
|
|
|
var msg ClientMessage
|
2015-10-28 15:19:22 -07:00
|
|
|
var messageType int
|
|
|
|
var packet []byte
|
2015-10-24 20:40:49 -07:00
|
|
|
var err error
|
2015-10-28 15:19:22 -07:00
|
|
|
for ; err == nil; messageType, packet, err = conn.ReadMessage() {
|
|
|
|
if messageType == websocket.BinaryMessage {
|
|
|
|
err = &CloseGotBinaryMessage
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if messageType == websocket.CloseMessage {
|
|
|
|
err = io.EOF
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
UnmarshalClientMessage(packet, messageType, &msg)
|
2015-10-24 20:40:49 -07:00
|
|
|
if msg.MessageID == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-29 01:30:25 -07:00
|
|
|
select {
|
|
|
|
case clientChan <- msg:
|
|
|
|
case <-stoppedChan:
|
|
|
|
close(errorChan)
|
|
|
|
close(clientChan)
|
|
|
|
return
|
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
}
|
2015-10-27 21:21:06 -07:00
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
_, isClose := err.(*websocket.CloseError)
|
|
|
|
if err != io.EOF && !isClose {
|
|
|
|
log.Println("Error while reading from client:", err)
|
2015-10-27 21:21:06 -07:00
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
errorChan <- err
|
|
|
|
close(errorChan)
|
|
|
|
close(clientChan)
|
|
|
|
// exit
|
2015-10-29 01:30:25 -07:00
|
|
|
}(_errorChan, _clientChan, stoppedChan)
|
2015-10-24 20:40:49 -07:00
|
|
|
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.SetPongHandler(func(pongBody string) error {
|
|
|
|
client.pingCount = 0
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
var errorChan <-chan error = _errorChan
|
|
|
|
var clientChan <-chan ClientMessage = _clientChan
|
|
|
|
var serverMessageChan <-chan ClientMessage = _serverMessageChan
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// All set up, now enter the work loop
|
|
|
|
|
2015-10-26 10:06:45 -07:00
|
|
|
RunLoop:
|
2015-10-24 20:40:49 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errorChan:
|
2015-10-28 15:19:22 -07:00
|
|
|
if err == io.EOF {
|
|
|
|
conn.Close() // no need to send a close frame :)
|
|
|
|
break RunLoop
|
|
|
|
} else if closeMsg, isClose := err.(*websocket.CloseError); isClose {
|
|
|
|
CloseConnection(conn, closeMsg)
|
|
|
|
} else {
|
|
|
|
CloseConnection(conn, &websocket.CloseError{
|
|
|
|
Code: websocket.CloseInternalServerErr,
|
|
|
|
Text: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
break RunLoop
|
2015-10-28 15:19:22 -07:00
|
|
|
|
2015-10-24 21:33:55 -07:00
|
|
|
case msg := <-clientChan:
|
|
|
|
if client.Version == "" && msg.Command != HelloCommand {
|
2015-10-28 15:49:53 -07:00
|
|
|
log.Println("error - first message wasn't hello from", conn.RemoteAddr(), "-", msg)
|
2015-10-28 17:31:44 -07:00
|
|
|
CloseConnection(conn, &CloseFirstMessageNotHello)
|
2015-10-24 21:33:55 -07:00
|
|
|
break RunLoop
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
HandleCommand(conn, &client, msg)
|
2015-10-28 22:59:27 -07:00
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
case smsg := <-serverMessageChan:
|
2015-10-28 15:19:22 -07:00
|
|
|
SendMessage(conn, smsg)
|
2015-10-28 22:59:27 -07:00
|
|
|
|
2015-11-01 13:17:35 -08:00
|
|
|
case <-time.After(1 * time.Minute):
|
2015-10-28 22:59:27 -07:00
|
|
|
client.pingCount++
|
|
|
|
if client.pingCount == 5 {
|
|
|
|
CloseConnection(conn, &CloseTimedOut)
|
|
|
|
break RunLoop
|
|
|
|
} else {
|
|
|
|
conn.WriteControl(websocket.PingMessage, []byte(strconv.FormatInt(time.Now().Unix(), 10)), getDeadline())
|
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
2015-10-25 03:21:50 -07:00
|
|
|
|
|
|
|
// Exit
|
|
|
|
|
|
|
|
// Launch message draining goroutine - we aren't out of the pub/sub records
|
|
|
|
go func() {
|
2015-10-26 10:06:45 -07:00
|
|
|
for _ = range _serverMessageChan {
|
|
|
|
}
|
2015-10-25 03:21:50 -07:00
|
|
|
}()
|
|
|
|
|
2015-10-29 01:30:25 -07:00
|
|
|
close(stoppedChan)
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// Stop getting messages...
|
|
|
|
UnsubscribeAll(&client)
|
|
|
|
|
2015-10-29 01:23:58 -07:00
|
|
|
client.MsgChannelKeepalive.Lock()
|
|
|
|
client.MessageChannel = nil
|
|
|
|
client.MsgChannelKeepalive.Unlock()
|
|
|
|
|
2015-10-25 03:21:50 -07:00
|
|
|
// And finished.
|
|
|
|
// Close the channel so the draining goroutine can finish, too.
|
|
|
|
close(_serverMessageChan)
|
2015-10-28 15:19:22 -07:00
|
|
|
|
|
|
|
log.Println("End socket connection from", conn.RemoteAddr())
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2015-10-28 22:59:27 -07:00
|
|
|
func getDeadline() time.Time {
|
|
|
|
return time.Now().Add(1 * time.Minute)
|
|
|
|
}
|
|
|
|
|
2015-10-24 21:33:55 -07:00
|
|
|
func CallHandler(handler CommandHandler, conn *websocket.Conn, client *ClientInfo, cmsg ClientMessage) (rmsg ClientMessage, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
fmt.Print("[!] Error executing command", cmsg.Command, "--", r)
|
|
|
|
err, ok = r.(error)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("command handler: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return handler(conn, client, cmsg)
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
func CloseConnection(conn *websocket.Conn, closeMsg *websocket.CloseError) {
|
2015-10-28 17:31:44 -07:00
|
|
|
if closeMsg != &CloseFirstMessageNotHello {
|
|
|
|
log.Println("Terminating connection with", conn.RemoteAddr(), "-", closeMsg.Text)
|
|
|
|
}
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(closeMsg.Code, closeMsg.Text), getDeadline())
|
2015-10-28 15:19:22 -07:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendMessage(conn *websocket.Conn, msg ClientMessage) {
|
|
|
|
messageType, packet, err := MarshalClientMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to marshal: %v %v", err, msg))
|
|
|
|
}
|
2015-10-28 22:59:27 -07:00
|
|
|
conn.SetWriteDeadline(getDeadline())
|
2015-10-28 15:19:22 -07:00
|
|
|
conn.WriteMessage(messageType, packet)
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
// Unpack a message sent from the client into a ClientMessage.
|
2015-10-28 15:19:22 -07:00
|
|
|
func UnmarshalClientMessage(data []byte, payloadType int, v interface{}) (err error) {
|
2015-10-24 19:59:34 -07:00
|
|
|
var spaceIdx int
|
|
|
|
|
|
|
|
out := v.(*ClientMessage)
|
|
|
|
dataStr := string(data)
|
|
|
|
|
|
|
|
// Message ID
|
|
|
|
spaceIdx = strings.IndexRune(dataStr, ' ')
|
|
|
|
if spaceIdx == -1 {
|
|
|
|
return ProtocolError
|
|
|
|
}
|
|
|
|
messageId, err := strconv.Atoi(dataStr[:spaceIdx])
|
2015-10-25 20:17:17 -07:00
|
|
|
if messageId < -1 || messageId == 0 {
|
|
|
|
return ProtocolErrorNegativeID
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out.MessageID = messageId
|
2015-10-26 10:06:45 -07:00
|
|
|
dataStr = dataStr[spaceIdx+1:]
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
spaceIdx = strings.IndexRune(dataStr, ' ')
|
|
|
|
if spaceIdx == -1 {
|
|
|
|
out.Command = Command(dataStr)
|
|
|
|
out.Arguments = nil
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
out.Command = Command(dataStr[:spaceIdx])
|
|
|
|
}
|
2015-10-26 10:06:45 -07:00
|
|
|
dataStr = dataStr[spaceIdx+1:]
|
2015-10-24 19:59:34 -07:00
|
|
|
argumentsJson := dataStr
|
2015-10-25 00:44:25 -07:00
|
|
|
out.origArguments = argumentsJson
|
2015-10-26 11:22:06 -07:00
|
|
|
err = out.parseOrigArguments()
|
2015-10-24 19:59:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:22:06 -07:00
|
|
|
func (cm *ClientMessage) parseOrigArguments() error {
|
|
|
|
err := json.Unmarshal([]byte(cm.origArguments), &cm.Arguments)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
func MarshalClientMessage(clientMessage interface{}) (payloadType int, data []byte, err error) {
|
2015-10-24 19:59:34 -07:00
|
|
|
var msg ClientMessage
|
|
|
|
var ok bool
|
|
|
|
msg, ok = clientMessage.(ClientMessage)
|
|
|
|
if !ok {
|
|
|
|
pMsg, ok := clientMessage.(*ClientMessage)
|
|
|
|
if !ok {
|
|
|
|
panic("MarshalClientMessage: argument needs to be a ClientMessage")
|
|
|
|
}
|
|
|
|
msg = *pMsg
|
|
|
|
}
|
|
|
|
var dataStr string
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
if msg.Command == "" && msg.MessageID == 0 {
|
|
|
|
panic("MarshalClientMessage: attempt to send an empty ClientMessage")
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
if msg.Command == "" {
|
|
|
|
msg.Command = SuccessCommand
|
|
|
|
}
|
2015-10-24 20:40:49 -07:00
|
|
|
if msg.MessageID == 0 {
|
|
|
|
msg.MessageID = -1
|
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
|
|
|
|
if msg.Arguments != nil {
|
|
|
|
argBytes, err := json.Marshal(msg.Arguments)
|
|
|
|
if err != nil {
|
2015-10-28 15:19:22 -07:00
|
|
|
return 0, nil, err
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
dataStr = fmt.Sprintf("%d %s %s", msg.MessageID, msg.Command, string(argBytes))
|
|
|
|
} else {
|
|
|
|
dataStr = fmt.Sprintf("%d %s", msg.MessageID, msg.Command)
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:19:22 -07:00
|
|
|
return websocket.TextMessage, []byte(dataStr), nil
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 20:40:49 -07:00
|
|
|
// Command handlers should use this to construct responses.
|
2015-11-01 00:26:46 -07:00
|
|
|
func SuccessMessageFromString(arguments string) ClientMessage {
|
|
|
|
cm := ClientMessage{
|
2015-11-01 13:17:35 -08:00
|
|
|
MessageID: -1, // filled by the select loop
|
|
|
|
Command: SuccessCommand,
|
2015-11-01 00:26:46 -07:00
|
|
|
origArguments: arguments,
|
2015-10-24 20:40:49 -07:00
|
|
|
}
|
2015-11-01 00:26:46 -07:00
|
|
|
cm.parseOrigArguments()
|
|
|
|
return cm
|
2015-10-24 20:40:49 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
// Convenience method: Parse the arguments of the ClientMessage as a single string.
|
|
|
|
func (cm *ClientMessage) ArgumentsAsString() (string1 string, err error) {
|
|
|
|
var ok bool
|
|
|
|
string1, ok = cm.Arguments.(string)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedSingleString
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
return string1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 00:44:25 -07:00
|
|
|
// Convenience method: Parse the arguments of the ClientMessage as a single int.
|
2015-10-26 14:55:20 -07:00
|
|
|
func (cm *ClientMessage) ArgumentsAsInt() (int1 int64, err error) {
|
2015-10-25 00:44:25 -07:00
|
|
|
var ok bool
|
|
|
|
var num float64
|
|
|
|
num, ok = cm.Arguments.(float64)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedSingleInt
|
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
} else {
|
2015-10-26 14:55:20 -07:00
|
|
|
int1 = int64(num)
|
2015-10-25 00:44:25 -07:00
|
|
|
return int1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:59:34 -07:00
|
|
|
// Convenience method: Parse the arguments of the ClientMessage as an array of two strings.
|
|
|
|
func (cm *ClientMessage) ArgumentsAsTwoStrings() (string1, string2 string, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedTwoStrings
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedTwoStrings
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
string1, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedTwoStrings
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
2015-10-28 16:07:51 -07:00
|
|
|
// clientID can be null
|
|
|
|
if ary[1] == nil {
|
|
|
|
return string1, "", nil
|
|
|
|
}
|
2015-10-24 19:59:34 -07:00
|
|
|
string2, ok = ary[1].(string)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedTwoStrings
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
return string1, string2, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience method: Parse the arguments of the ClientMessage as an array of a string and an int.
|
|
|
|
func (cm *ClientMessage) ArgumentsAsStringAndInt() (string1 string, int int64, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndInt
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndInt
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
string1, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndInt
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
var num float64
|
|
|
|
num, ok = ary[1].(float64)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndInt
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
int = int64(num)
|
|
|
|
if float64(int) != num {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndIntGotFloat
|
|
|
|
return
|
2015-10-24 19:59:34 -07:00
|
|
|
}
|
|
|
|
return string1, int, nil
|
|
|
|
}
|
|
|
|
}
|
2015-10-25 00:44:25 -07:00
|
|
|
|
|
|
|
// Convenience method: Parse the arguments of the ClientMessage as an array of a string and an int.
|
|
|
|
func (cm *ClientMessage) ArgumentsAsStringAndBool() (str string, flag bool, err error) {
|
|
|
|
var ok bool
|
|
|
|
var ary []interface{}
|
|
|
|
ary, ok = cm.Arguments.([]interface{})
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndBool
|
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
} else {
|
|
|
|
if len(ary) != 2 {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndBool
|
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
str, ok = ary[0].(string)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndBool
|
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
flag, ok = ary[1].(bool)
|
|
|
|
if !ok {
|
2015-10-26 10:06:45 -07:00
|
|
|
err = ExpectedStringAndBool
|
|
|
|
return
|
2015-10-25 00:44:25 -07:00
|
|
|
}
|
|
|
|
return str, flag, nil
|
|
|
|
}
|
|
|
|
}
|