mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-07-01 08:38:32 +00:00
Add TwitchUsername to ClientInfo
This commit is contained in:
parent
6681c1d64b
commit
9a1d1b720d
2 changed files with 24 additions and 12 deletions
|
@ -5,8 +5,13 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var EmptyClientMessage ClientMessage = ClientMessage{}
|
||||||
|
|
||||||
func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||||
version, clientId, err := msg.ArgumentsAsTwoStrings()
|
version, clientId, err := msg.ArgumentsAsTwoStrings()
|
||||||
|
if err != nil {
|
||||||
|
return EmptyClientMessage, nil
|
||||||
|
}
|
||||||
|
|
||||||
client.Version = version
|
client.Version = version
|
||||||
client.ClientID = uuid.FromStringOrNil(clientId)
|
client.ClientID = uuid.FromStringOrNil(clientId)
|
||||||
|
@ -20,6 +25,7 @@ func HandleHello(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (r
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleSetUser(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
func HandleSetUser(conn *websocket.Conn, client *ClientInfo, msg ClientMessage) (rmsg ClientMessage, err error) {
|
||||||
|
username, err := msg.ArgumentsAsString()
|
||||||
|
|
||||||
return ClientMessage{}, nil
|
return ClientMessage{}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,8 @@ type ClientMessage struct {
|
||||||
// When replying to a command, the message ID must be echoed.
|
// When replying to a command, the message ID must be echoed.
|
||||||
// When sending a server-initiated message, this is -1.
|
// When sending a server-initiated message, this is -1.
|
||||||
MessageID int
|
MessageID int
|
||||||
// The command that the client wants from the server.
|
// The command that the client wants from the server.
|
||||||
// When sent from the server, the literal string 'True' indicates success.
|
// When sent from the server, the literal string 'True' indicates success.
|
||||||
// Before sending, a blank Command will be converted into SuccessCommand.
|
// Before sending, a blank Command will be converted into SuccessCommand.
|
||||||
Command Command
|
Command Command
|
||||||
//
|
//
|
||||||
|
@ -43,28 +43,34 @@ type ClientMessage struct {
|
||||||
type ClientInfo struct {
|
type ClientInfo struct {
|
||||||
// The client ID.
|
// The client ID.
|
||||||
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
|
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
|
||||||
ClientID uuid.UUID
|
ClientID uuid.UUID
|
||||||
|
|
||||||
// The client's version.
|
// The client's version.
|
||||||
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
|
// This must be written once by the owning goroutine before the struct is passed off to any other goroutines.
|
||||||
Version string
|
Version string
|
||||||
|
|
||||||
// This mutex protects writable data in this struct.
|
// This mutex protects writable data in this struct.
|
||||||
// If it seems to be a performance problem, we can split this.
|
// If it seems to be a performance problem, we can split this.
|
||||||
Mutex sync.Mutex
|
Mutex sync.Mutex
|
||||||
|
|
||||||
|
// The client's claimed username on Twitch.
|
||||||
|
TwitchUsername string
|
||||||
|
|
||||||
|
// Whether or not the server has validated the client's claimed username.
|
||||||
|
UsernameValidated bool
|
||||||
|
|
||||||
// The list of chats this client is currently in.
|
// The list of chats this client is currently in.
|
||||||
// Protected by Mutex
|
// Protected by Mutex
|
||||||
CurrentChannels []string
|
CurrentChannels []string
|
||||||
|
|
||||||
// Server-initiated messages should be sent here
|
// Server-initiated messages should be sent here
|
||||||
MessageChannel chan<- ClientMessage
|
MessageChannel chan <- ClientMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
// A function that is called to respond to a Command.
|
// A function that is called to respond to a Command.
|
||||||
type CommandHandler func(*websocket.Conn, *ClientInfo, ClientMessage) (ClientMessage, error)
|
type CommandHandler func(*websocket.Conn, *ClientInfo, ClientMessage) (ClientMessage, error)
|
||||||
|
|
||||||
var CommandHandlers = map[Command]CommandHandler {
|
var CommandHandlers = map[Command]CommandHandler{
|
||||||
HelloCommand: HandleHello,
|
HelloCommand: HandleHello,
|
||||||
"get_display_name": HandleGetDisplayName,
|
"get_display_name": HandleGetDisplayName,
|
||||||
"sub": HandleSub,
|
"sub": HandleSub,
|
||||||
|
@ -154,7 +160,7 @@ func HandleSocketConnection(conn *websocket.Conn) {
|
||||||
_errorChan := make(chan error)
|
_errorChan := make(chan error)
|
||||||
|
|
||||||
// Receive goroutine
|
// Receive goroutine
|
||||||
go func(errorChan chan<- error, clientChan chan<- ClientMessage) {
|
go func(errorChan chan <- error, clientChan chan <- ClientMessage) {
|
||||||
var msg ClientMessage
|
var msg ClientMessage
|
||||||
var err error
|
var err error
|
||||||
for ; err == nil; err = FFZCodec.Receive(conn, &msg) {
|
for ; err == nil; err = FFZCodec.Receive(conn, &msg) {
|
||||||
|
@ -180,13 +186,11 @@ func HandleSocketConnection(conn *websocket.Conn) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-errorChan:
|
case err := <-errorChan:
|
||||||
// note - socket might not be open at this point
|
|
||||||
// don't care
|
|
||||||
FFZCodec.Send(conn, ClientMessage{
|
FFZCodec.Send(conn, ClientMessage{
|
||||||
MessageID: -1,
|
MessageID: -1,
|
||||||
Command: "error",
|
Command: "error",
|
||||||
Arguments: err.Error(),
|
Arguments: err.Error(),
|
||||||
})
|
}) // note - socket might be closed, but don't care
|
||||||
break RunLoop
|
break RunLoop
|
||||||
case msg := <-clientChan:
|
case msg := <-clientChan:
|
||||||
if client.Version == "" && msg.Command != HelloCommand {
|
if client.Version == "" && msg.Command != HelloCommand {
|
||||||
|
@ -206,6 +210,8 @@ func HandleSocketConnection(conn *websocket.Conn) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println(conn.RemoteAddr(), msg.MessageID, msg.Command, msg.Arguments)
|
||||||
|
|
||||||
client.Mutex.Lock()
|
client.Mutex.Lock()
|
||||||
response, err := CallHandler(handler, conn, &client, msg)
|
response, err := CallHandler(handler, conn, &client, msg)
|
||||||
client.Mutex.Unlock()
|
client.Mutex.Unlock()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue