diff --git a/src/command.ts b/src/command.ts index b10db3c..d49df42 100644 --- a/src/command.ts +++ b/src/command.ts @@ -25,6 +25,14 @@ function unmask_room_name(name: string) { return null; } +export function send_message({ msg, sender }: { msg: string; sender: string; }, io: SocketServer, socket: Socket) { + io.in(Array.from(socket.rooms)).emit('new message', { + type: 'text-message', + data: msg, + sender: sender, + }); +} + export function run_command(cmd_raw: string, uid: string, users: Map, io: SocketServer, silent: boolean = false) { const cmd = cmd_raw.replace(/(^\s*)|(\s*$)/, ''); @@ -172,11 +180,7 @@ export function run_command(cmd_raw: string, uid: string, users: Map { - io.in(Array.from(socket.rooms)).emit('new message', { - type: 'text-message', - data: msg, - sender: 'Anonymous User', - }); + send_message({ msg: msg, sender: 'Anonymous User'}, io, socket); }); if (user.is_administrator) { diff --git a/src/index.ts b/src/index.ts index 1307d68..4d0eff6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import http_server = require('http'); import path = require('path'); import { kill_username, register_username, User } from './user'; -import { is_command, run_command } from './command'; +import { is_command, run_command, send_message } from './command'; const app = Express(); const http = new http_server.Server(app); @@ -28,11 +28,10 @@ io.on('connection', socket => { return; } - io.in(Array.from(socket.rooms)).emit('new message', { - type: 'text-message', - data: msg, + send_message({ + msg: msg, sender: user.name, - }); + }, io, socket); }); socket.on('set-name', name => {