Flesh out the JSON stuff a bit more
Implement some commands in the wasm client code: - /playing (setting and clearing) - /refreshplayer Commands not yet implemented: - /purge (command doesn't exist yet on server) - /help (doesn't open window)
This commit is contained in:
parent
568344a794
commit
bdd4e71261
|
@ -8,7 +8,7 @@ import (
|
|||
"unicode"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
//"github.com/zorchenhimer/MovieNight/common"
|
||||
"github.com/zorchenhimer/MovieNight/common"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
@ -115,7 +115,13 @@ func (cl *Client) Send(msgs string) {
|
|||
// Send server message to this client
|
||||
func (cl *Client) ServerMessage(msg string) {
|
||||
msg = ParseEmotes(msg)
|
||||
cl.Send(`<span class="svmsg">` + msg + `</span><br />`)
|
||||
encoded, err := common.EncodeMessage("", "#ea6260", msg, common.MSG_ERROR)
|
||||
if err != nil {
|
||||
fmt.Printf("Error encoding server message to %s: %s; Message: %s\n", cl.name, err, msg)
|
||||
return
|
||||
}
|
||||
cl.Send(encoded)
|
||||
//cl.Send(`<span class="svmsg">` + msg + `</span><br />`)
|
||||
}
|
||||
|
||||
// Outgoing messages
|
||||
|
|
17
chatroom.go
17
chatroom.go
|
@ -220,12 +220,21 @@ func (cr *ChatRoom) Ban(name string) string {
|
|||
|
||||
// Add a chat message from a viewer
|
||||
func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) {
|
||||
data, err := common.EncodeChat(
|
||||
t := common.MSG_CHAT
|
||||
|
||||
if isAction {
|
||||
t = common.MSG_ACTION
|
||||
}
|
||||
|
||||
if isServer {
|
||||
t = common.MSG_SERVER
|
||||
}
|
||||
|
||||
data, err := common.EncodeMessage(
|
||||
from.name,
|
||||
from.color,
|
||||
msg,
|
||||
isAction,
|
||||
isServer)
|
||||
t)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error encoding chat message: %s", err)
|
||||
|
@ -233,8 +242,6 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) {
|
|||
return
|
||||
}
|
||||
|
||||
fmt.Println("Chat encoded OK")
|
||||
|
||||
cr.queue <- data
|
||||
}
|
||||
|
||||
|
|
|
@ -16,12 +16,11 @@ type DataError struct {
|
|||
Message string
|
||||
}
|
||||
|
||||
type DataChat struct {
|
||||
From string
|
||||
Color string
|
||||
Message string
|
||||
IsAction bool
|
||||
IsServer bool // server message?
|
||||
type DataMessage struct {
|
||||
From string
|
||||
Color string
|
||||
Message string
|
||||
Type MessageType
|
||||
}
|
||||
|
||||
type DataCommand struct {
|
||||
|
@ -40,11 +39,7 @@ type DataInterface interface {
|
|||
HTML() string
|
||||
}
|
||||
|
||||
type VisibleData interface {
|
||||
HTML() string
|
||||
}
|
||||
|
||||
func (dc DataChat) GetType() DataType {
|
||||
func (dc DataMessage) GetType() DataType {
|
||||
return DT_CHAT
|
||||
}
|
||||
|
||||
|
@ -86,6 +81,7 @@ const (
|
|||
CMD_PLAYING CommandType = iota
|
||||
CMD_REFRESHPLAYER
|
||||
CMD_PURGECHAT
|
||||
CMD_HELP
|
||||
)
|
||||
|
||||
type EventType int
|
||||
|
@ -98,35 +94,49 @@ const (
|
|||
EV_SERVERMESSAGE
|
||||
)
|
||||
|
||||
type MessageType int
|
||||
|
||||
const (
|
||||
MSG_CHAT MessageType = iota // standard chat
|
||||
MSG_ACTION // /me command
|
||||
MSG_SERVER // server message
|
||||
MSG_ERROR
|
||||
)
|
||||
|
||||
// TODO: Read this HTML from a template somewhere
|
||||
func (dc DataChat) HTML() string {
|
||||
if dc.IsAction {
|
||||
func (dc DataMessage) HTML() string {
|
||||
fmt.Printf("message type: %d\n", dc.Type)
|
||||
switch dc.Type {
|
||||
case MSG_ACTION:
|
||||
return `<span style="color:` + dc.Color + `"><span class="name">` + dc.From +
|
||||
`</span> <span class="cmdme">` + dc.Message + `</span><br />`
|
||||
}
|
||||
|
||||
if dc.IsServer {
|
||||
case MSG_SERVER:
|
||||
return `<div class="announcement">` + dc.Message + `</div>`
|
||||
}
|
||||
|
||||
return `<span class="name" style="color:` + dc.Color + `">` + dc.From +
|
||||
`</span><b>:</b> <span class="msg">` + dc.Message + `</span><br />`
|
||||
case MSG_ERROR:
|
||||
return `<div class="error">` + dc.Message + `</div>`
|
||||
|
||||
default:
|
||||
return `<span class="name" style="color:` + dc.Color + `">` + dc.From +
|
||||
`</span><b>:</b> <span class="msg">` + dc.Message + `</span><br />`
|
||||
}
|
||||
}
|
||||
|
||||
func (de DataEvent) HTML() string {
|
||||
switch de.Event {
|
||||
case EV_KICK:
|
||||
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
|
||||
de.User + `</b> has been kicked.</span><br />`
|
||||
de.User + `</span> has been kicked.<br />`
|
||||
case EV_LEAVE:
|
||||
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
|
||||
de.User + `</b> has left the chat.</span><br />`
|
||||
de.User + `</span> has left the chat.<br />`
|
||||
case EV_BAN:
|
||||
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
|
||||
de.User + `</b> has been banned.</span><br />`
|
||||
de.User + `</span> has been banned.<br />`
|
||||
case EV_JOIN:
|
||||
return `<span class="event"><span class="name" style="color:` + de.Color + `">` +
|
||||
de.User + `</b> has joined the chat.</span><br />`
|
||||
de.User + `</span> has joined the chat.<br />`
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
@ -139,19 +149,17 @@ func (de DataCommand) HTML() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func EncodeChat(name, color, msg string, isAction, isServer bool) (string, error) {
|
||||
func EncodeMessage(name, color, msg string, msgtype MessageType) (string, error) {
|
||||
d := ChatData{
|
||||
Type: DT_CHAT,
|
||||
Data: DataChat{
|
||||
From: name,
|
||||
Color: color,
|
||||
IsAction: isAction,
|
||||
IsServer: isServer,
|
||||
Message: msg,
|
||||
Data: DataMessage{
|
||||
From: name,
|
||||
Color: color,
|
||||
Message: msg,
|
||||
Type: msgtype,
|
||||
},
|
||||
}
|
||||
j, err := jsonifyChatData(d)
|
||||
fmt.Printf("Err: %s; data: %s\n", err, j)
|
||||
return j, err
|
||||
}
|
||||
|
||||
|
@ -199,18 +207,16 @@ func DecodeData(rawjson string) (DataInterface, error) {
|
|||
decoder := json.NewDecoder(strings.NewReader(rawjson))
|
||||
|
||||
// Open bracket
|
||||
t, err := decoder.Token()
|
||||
_, err := decoder.Token()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Open bracket token error: %s", err)
|
||||
}
|
||||
fmt.Printf("Token: %q\n", t)
|
||||
|
||||
for decoder.More() {
|
||||
key, err := decoder.Token()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error decoding token: %s", err)
|
||||
}
|
||||
fmt.Printf("Key: %q\n", key)
|
||||
|
||||
if fmt.Sprintf("%s", key) == "Type" {
|
||||
value, err := decoder.Token()
|
||||
|
@ -222,20 +228,13 @@ func DecodeData(rawjson string) (DataInterface, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("Error parsing data type: %d", data.Type)
|
||||
}
|
||||
fmt.Printf("data.Type: %d\n", data.Type)
|
||||
} else {
|
||||
fmt.Printf("Key: %q\n", key)
|
||||
//value, err := decoder.Token()
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("Error decoding value token: %s", err)
|
||||
//}
|
||||
//fmt.Printf("Value: %q", value)
|
||||
|
||||
switch DataType(data.Type) {
|
||||
case DT_CHAT:
|
||||
d := DataChat{}
|
||||
d := DataMessage{}
|
||||
if err := decoder.Decode(&d); err != nil {
|
||||
return nil, fmt.Errorf("Unable to decode DataChat: %s", err)
|
||||
return nil, fmt.Errorf("Unable to decode DataMessage: %s", err)
|
||||
}
|
||||
return d, nil
|
||||
case DT_ERROR:
|
||||
|
@ -260,12 +259,6 @@ func DecodeData(rawjson string) (DataInterface, error) {
|
|||
return nil, fmt.Errorf("Invalid data type: %d", data.Type)
|
||||
}
|
||||
|
||||
//fmt.Printf("di.GetType: %q\n", di.GetType())
|
||||
//err = decoder.Decode(&di)
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("Unable to decode data into interface: %s", err)
|
||||
//}
|
||||
//return di, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Incomplete data")
|
||||
|
|
|
@ -26,19 +26,32 @@ func recieve(v []js.Value) {
|
|||
return
|
||||
}
|
||||
|
||||
//dt, err := common.ParseDataType(*data.Type)
|
||||
//if err != nil {
|
||||
// fmt.Printf("Error decoding type: %s\n", err)
|
||||
// js.Call("appendMessages", v)
|
||||
// return
|
||||
//}
|
||||
|
||||
switch data.GetType() {
|
||||
case common.DT_CHAT, common.DT_EVENT, common.DT_ERROR:
|
||||
fmt.Printf("data raw: %q\n", data)
|
||||
dc := common.VisibleData(data)
|
||||
js.Call("appendMessages", dc.HTML())
|
||||
js.Call("appendMessages", data.HTML())
|
||||
case common.DT_COMMAND:
|
||||
dc := data.(common.DataCommand)
|
||||
|
||||
switch dc.Command {
|
||||
case common.CMD_PLAYING:
|
||||
if dc.Arguments == nil || len(dc.Arguments) == 0 {
|
||||
js.Call("setPlaying", "", "")
|
||||
|
||||
} else if len(dc.Arguments) == 1 {
|
||||
js.Call("setPlaying", dc.Arguments[0], "")
|
||||
|
||||
} else if len(dc.Arguments) == 2 {
|
||||
js.Call("setPlaying", dc.Arguments[0], dc.Arguments[1])
|
||||
}
|
||||
case common.CMD_REFRESHPLAYER:
|
||||
js.Call("initPlayer", nil)
|
||||
case common.CMD_PURGECHAT:
|
||||
fmt.Println("//TODO: chat purge command received.")
|
||||
case common.CMD_HELP:
|
||||
js.Call("appendMesages", data.HTML())
|
||||
// TODO: open window
|
||||
//js.Call("")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue