Browse Source

chat client provides its initial username when it connects

this keeps the client and server view of the chat clients
more consistent so consumers of the API
(for example, /api/admin/clients) will get more accurate data
initial-username
forest 1 month ago
parent
commit
8b1bb2866b
4 changed files with 20 additions and 7 deletions
  1. +6
    -1
      core/chat/client.go
  2. +3
    -2
      webroot/js/app-standalone-chat.js
  3. +3
    -2
      webroot/js/app.js
  4. +8
    -2
      webroot/js/utils/websocket.js

+ 6
- 1
core/chat/client.go View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
log "github.com/sirupsen/logrus"
@ -48,11 +49,15 @@ func NewClient(ws *websocket.Conn) *Client {
log.Panicln("ws cannot be nil")
}
var initialUsername string
var ignoreClient = false
for _, extraData := range ws.Config().Protocol {
if extraData == "IGNORE_CLIENT" {
ignoreClient = true
}
if strings.HasPrefix(extraData, "INITIAL_USERNAME") {
initialUsername = strings.TrimPrefix(extraData, "INITIAL_USERNAME")
}
}
ch := make(chan models.ChatEvent, channelBufSize)
@ -68,7 +73,7 @@ func NewClient(ws *websocket.Conn) *Client {
rateLimiter := rate.NewLimiter(0.6, 5)
return &Client{time.Now(), 0, userAgent, ipAddress, nil, clientID, nil, ignoreClient, socketID, ws, ch, pingch, usernameChangeChannel, userJoinedChannel, doneCh, rateLimiter}
return &Client{time.Now(), 0, userAgent, ipAddress, &initialUsername, clientID, nil, ignoreClient, socketID, ws, ch, pingch, usernameChangeChannel, userJoinedChannel, doneCh, rateLimiter}
}
func (c *Client) write(msg models.ChatEvent) {


+ 3
- 2
webroot/js/app-standalone-chat.js View File

@ -11,10 +11,11 @@ export default class StandaloneChat extends Component {
constructor(props, context) {
super(props, context);
const initialUsername = getLocalStorage(KEY_USERNAME) || generateUsername();
this.state = {
websocket: new Websocket(true), // Send along the "ignoreClient" flag so this isn't counted as a viewer
websocket: new Websocket(true, initialUsername), // Send along the "ignoreClient" flag so this isn't counted as a viewer
chatEnabled: true, // always true for standalone chat
username: getLocalStorage(KEY_USERNAME) || generateUsername(),
username: initialUsername,
};
this.websocket = null;


+ 3
- 2
webroot/js/app.js View File

@ -54,12 +54,13 @@ export default class App extends Component {
this.hasTouchScreen = hasTouchScreen();
this.windowBlurred = false;
const initialUsername = getLocalStorage(KEY_USERNAME) || generateUsername();
this.state = {
websocket: new Websocket(),
websocket: new Websocket(false, initialUsername),
displayChat: chatStorage === null ? true : chatStorage,
chatInputEnabled: false, // chat input box state
chatDisabled: false,
username: getLocalStorage(KEY_USERNAME) || generateUsername(),
username: initialUsername,
touchKeyboardActive: false,
configData: {},


+ 8
- 2
webroot/js/utils/websocket.js View File

@ -25,7 +25,7 @@ export const CALLBACKS = {
const TIMER_WEBSOCKET_RECONNECT = 5000; // ms
export default class Websocket {
constructor(ignoreClient) {
constructor(ignoreClient, initialUsername) {
this.websocket = null;
this.websocketReconnectTimer = null;
@ -43,7 +43,13 @@ export default class Websocket {
}
createAndConnect() {
const extraFlags = this.ignoreClient ? [IGNORE_CLIENT_FLAG] : [];
const extraFlags = [];
if(this.ignoreClient) {
extraFlags.push(IGNORE_CLIENT_FLAG);
}
if(this.initialUsername) {
extraFlags.push(`INITIAL_USERNAME_${initialUsername}`);
}
const ws = new WebSocket(URL_WEBSOCKET, extraFlags);
ws.onopen = this.onOpen.bind(this);
ws.onclose = this.onClose.bind(this);


Loading…
Cancel
Save