diff --git a/protocol/src/protocol.ts b/protocol/src/protocol.ts index a310015..1298611 100644 --- a/protocol/src/protocol.ts +++ b/protocol/src/protocol.ts @@ -37,6 +37,7 @@ export interface MSAgentInitMessage extends MSAgentProtocolMessage { data: { username: string agent: string + charlimit: number users: { username: string, agent: string diff --git a/server/config.example.toml b/server/config.example.toml index 7d535b3..1d23846 100644 --- a/server/config.example.toml +++ b/server/config.example.toml @@ -2,6 +2,9 @@ host = "127.0.0.1" port = 3000 +[chat] +charlimit = 100 + [tts] enabled = true # https://git.computernewb.com/computernewb/SAPIServer diff --git a/server/src/client.ts b/server/src/client.ts index c79272f..698e3de 100644 --- a/server/src/client.ts +++ b/server/src/client.ts @@ -41,6 +41,10 @@ export class Client extends EventEmitter { send(msg: MSAgentProtocolMessage) { return new Promise((res, rej) => { + if (this.socket.readyState !== WebSocket.OPEN) { + res(); + return; + } this.socket.send(JSON.stringify(msg), err => { if (err) { rej(err); @@ -66,7 +70,15 @@ export class Client extends EventEmitter { this.socket.close(); return; } - this.username = htmlentities.encode(joinMsg.data.username); + let username = htmlentities.encode(joinMsg.data.username); + if (this.room.clients.some(u => u.username === username)) { + let i = 1; + let uo = username; + do { + username = uo + i++; + } while (this.room.clients.some(u => u.username === username)) + } + this.username = username; this.agent = htmlentities.encode(joinMsg.data.agent); this.emit('join'); break; @@ -76,7 +88,8 @@ export class Client extends EventEmitter { if (!talkMsg.data || !talkMsg.data.msg) { return; } - this.emit('talk', htmlentities.encode(talkMsg.data.msg)); + if (talkMsg.data.msg.length > this.room.config.charlimit) return; + this.emit('talk', talkMsg.data.msg); break; } } diff --git a/server/src/config.ts b/server/src/config.ts index 8feadcf..5314967 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -3,6 +3,7 @@ export interface IConfig { host: string; port: number; } + chat: ChatConfig; tts: TTSConfig } @@ -12,4 +13,8 @@ export interface TTSConfig { voice: string; tempDir: string; wavExpirySeconds: number; +} + +export interface ChatConfig { + charlimit: number; } \ No newline at end of file diff --git a/server/src/index.ts b/server/src/index.ts index 52eb36b..6e224b9 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -7,6 +7,8 @@ import * as toml from 'toml'; import { IConfig } from './config.js'; import * as fs from 'fs'; import { TTSClient } from './tts.js'; +import path from 'path'; +import { fileURLToPath } from 'url'; let config: IConfig; let configPath: string; @@ -36,15 +38,23 @@ app.register(FastifyWS); let tts = null; +app.register(FastifyStatic, { + root: path.dirname(fileURLToPath(import.meta.url)) + "/../../webapp/dist/", + prefix: "/", + decorateReply: true + }); + if (config.tts.enabled) { tts = new TTSClient(config.tts); app.register(FastifyStatic, { root: config.tts.tempDir, - prefix: "/api/tts/" + prefix: "/api/tts/", + decorateReply: false }); } -let room = new MSAgentChatRoom(tts); + +let room = new MSAgentChatRoom(config.chat, tts); app.register(async app => { app.get("/socket", {websocket: true}, (socket, req) => { diff --git a/server/src/room.ts b/server/src/room.ts index 8d4bdd0..c9d2266 100644 --- a/server/src/room.ts +++ b/server/src/room.ts @@ -1,13 +1,18 @@ import { MSAgentAddUserMessage, MSAgentChatMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol"; import { Client } from "./client.js"; import { TTSClient } from "./tts.js"; +import { ChatConfig } from "./config.js"; +import * as htmlentities from 'html-entities'; export class MSAgentChatRoom { clients: Client[]; tts: TTSClient | null; msgId : number = 0; - constructor(tts: TTSClient | null) { + config: ChatConfig; + + constructor(config: ChatConfig, tts: TTSClient | null) { this.clients = []; + this.config = config; this.tts = tts; } @@ -32,6 +37,7 @@ export class MSAgentChatRoom { data: { username: client.username!, agent: client.agent!, + charlimit: this.config.charlimit, users: this.clients.filter(c => c.username !== null).map(c => { return { username: c.username!, @@ -57,7 +63,7 @@ export class MSAgentChatRoom { op: MSAgentProtocolMessageType.Chat, data: { username: client.username!, - message + message: htmlentities.encode(message) } }; if (this.tts !== null) { diff --git a/webapp/src/ts/client.ts b/webapp/src/ts/client.ts index 3af4ae7..26b1f00 100644 --- a/webapp/src/ts/client.ts +++ b/webapp/src/ts/client.ts @@ -16,6 +16,7 @@ export class MSAgentClient { private events: Emitter; private users: User[]; private playingAudio: Map = new Map(); + private charlimit: number = 0; private username: string | null = null; private agent: string | null = null; @@ -99,6 +100,10 @@ export class MSAgentClient { this.send(talkMsg); } + getCharlimit() { + return this.charlimit; + } + private handleMessage(data: string) { let msg: MSAgentProtocolMessage; try { @@ -112,6 +117,7 @@ export class MSAgentClient { let initMsg = msg as MSAgentInitMessage; this.username = initMsg.data.username; this.agent = initMsg.data.agent; + this.charlimit = initMsg.data.charlimit; this.users.push(...initMsg.data.users.map(u => new User(u.username, u.agent))); this.events.emit('join'); break; @@ -119,6 +125,7 @@ export class MSAgentClient { case MSAgentProtocolMessageType.AddUser: { let addUserMsg = msg as MSAgentAddUserMessage let user = new User(addUserMsg.data.username, addUserMsg.data.agent); + this.users.push(user); this.events.emit('adduser', user); break; } diff --git a/webapp/src/ts/main.ts b/webapp/src/ts/main.ts index 6c5c9df..514c490 100644 --- a/webapp/src/ts/main.ts +++ b/webapp/src/ts/main.ts @@ -39,9 +39,10 @@ elements.chatSendBtn.addEventListener('click', () => { }); async function connectToRoom() { - Room = new MSAgentClient("http://127.0.0.1:3000"); + Room = new MSAgentClient(`${window.location.protocol}//${window.location.host}`); await Room.connect(); await Room.join(elements.logonUsername.value, "test"); + elements.chatInput.maxLength = Room.getCharlimit(); logonWindow.hide(); elements.logonView.style.display = "none"; elements.chatView.style.display = "block";