[refactory] extract function send_message

This commit is contained in:
方而静 2021-03-26 18:04:57 +08:00
parent 1abd6ecc2d
commit 19f7274e4b
No known key found for this signature in database
GPG Key ID: C4F2853BC3103681
2 changed files with 13 additions and 10 deletions

View File

@ -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<string, User>, 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<string, Use
});
command_map.set('anon', () => {
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) {

View File

@ -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 => {