Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

Add panel where users can be listed as they join. #1

Merged
merged 10 commits into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/client/public/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@
height: 300px;
margin: 0 0 10px 0;
}

#panel {
position: absolute;
top: 8px;
}

#panelUsers {
width: 250px;
resize: none;
height: 300px;
margin: 0 0 10px 0;
}
3 changes: 3 additions & 0 deletions src/client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<button id="btnSend">Send</button>
<button id="btnLeave">Leave</button>
</div>
<div id="panel">
<textarea id="panelUsers" readonly="1"></textarea>
</div>
<script src="index.js"></script>
</body>
</html>
48 changes: 47 additions & 1 deletion src/client/public/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
$(function onload() {
const ws = new WebSocket(`ws://${location.host}`);
let username = '';
let tArrUsers = [];

const objLogin = $('#login');
const objChat = $('#chat');
const objPanel = $('#panel');
const txtUsername = $('#txtUsername');
const btnJoin = $('#btnJoin');
const txtChat = $('#txtChat');
const txtMessage = $('#txtMessage');
const btnSend = $('#btnSend');
const btnLeave = $('#btnLeave');
const panelUsers = $('#panelUsers');

const chatScroll = () => {
txtChat.prop('scrollTop', txtChat.prop('scrollHeight'));
Expand All @@ -27,7 +30,29 @@ $(function onload() {
chatScroll();
};

const panelEmpty = () => {
panelUsers.val('');
};

const addUser = (pUserName) => {
tArrUsers.push(pUserName);
panelAddUsers(tArrUsers);
};

const removeUser = (pUserName) => {
tArrUsers.splice(tArrUsers.indexOf(pUserName), 1);
panelAddUsers(tArrUsers);
};

const panelAddUsers = (pArr) => {
panelEmpty();
pArr.sort().forEach((pName, pIndex) => {
panelUsers.val(`${panelUsers.val()}${pIndex + 1}. ${pName}\n`);
});
};

objChat.hide();
objPanel.hide();
txtUsername.focus();

ws.addEventListener('message', (evt) => {
Expand All @@ -40,10 +65,13 @@ $(function onload() {
username = s;
objLogin.hide();
objChat.show();
objPanel.show();
txtUsername.val('');
chatEmpty();
panelEmpty();
txtMessage.focus();
chatWriteLine(`You are connected! (${s})`);
getUsers();
} else {
alert(r);
txtUsername.focus();
Expand All @@ -66,18 +94,26 @@ $(function onload() {
const r = objData.r;
if (r === 'OK') {
chatEmpty();
panelEmpty();
username = '';
objLogin.show();
objChat.hide();
objPanel.hide();
txtUsername.val('').focus();
} else {
alert(r);
}
break;
}
case 'getUsersResp': {
tArrUsers = objData.userList;
panelAddUsers(tArrUsers);
break;
}
case 'join': {
const username = objData.d;
chatWriteLine(`User connect (${username})`);
addUser(username);
break;
}
case 'chat': {
Expand All @@ -89,6 +125,7 @@ $(function onload() {
case 'leave': {
const username = objData.d;
chatWriteLine(`User disconnect (${username})`);
removeUser(username);
break;
}
default: {
Expand All @@ -97,6 +134,14 @@ $(function onload() {
}
});

const getUsers = () => {
ws.send(
JSON.stringify({
h: 'getUsers',
})
);
};

btnJoin.click(() => {
const username = txtUsername.val();
ws.send(
Expand All @@ -108,7 +153,7 @@ $(function onload() {
});

btnSend.click(() => {
const message = txtMessage.val();
const message = $.trim(txtMessage.val());
if (message !== '') {
ws.send(
JSON.stringify({
Expand All @@ -117,6 +162,7 @@ $(function onload() {
})
);
} else {
txtMessage.val('');
txtMessage.focus();
}
});
Expand Down
26 changes: 24 additions & 2 deletions src/server/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ import { UPPERCASE_USERNAMES } from './Configuration.ts';
type WSMsgJoin = { h: 'join'; d: string };
type WSMsgLeave = { h: 'leave'; d: string };
type WSMsgChat = { h: 'chat'; s: string; d: string };
type WSMessageClient = WSMsgJoin | WSMsgLeave | WSMsgChat;
type WSMsgGetUsers = { h: 'getUsers' };
type WSMessageClient = WSMsgJoin | WSMsgLeave | WSMsgChat | WSMsgGetUsers;

type WSMsgJoinResp = { h: 'joinResp'; s: string; r: string };
type WSMsgLeaveResp = { h: 'leaveResp'; r: string };
type WSMsgChatResp = { h: 'chatResp'; d: string; r: string };
type WSMessageServer = WSMsgJoinResp | WSMsgLeaveResp | WSMsgChatResp;
type WSMsgGetUsersResp = {
h: 'getUsersResp';
userList: Array<string>;
};
type WSMessageServer =
| WSMsgJoinResp
| WSMsgLeaveResp
| WSMsgChatResp
| WSMsgGetUsersResp;

type WSMessage = WSMessageClient | WSMessageServer;

Expand Down Expand Up @@ -77,6 +86,10 @@ export async function HandleWSConn(pWebSocket: WebSocket): Promise<void> {
}
break;
}
case 'getUsers': {
await RespondGetUsers(_connInfo);
break;
}
default: {
console.log('Invalid message', objEvent);
break;
Expand Down Expand Up @@ -129,6 +142,15 @@ async function RespondChat(
});
}

async function RespondGetUsers(pConnInfo: ConnInfo) {
return Respond(pConnInfo, {
h: 'getUsersResp',
userList: (await GetConnections()).map(
(pConnection) => pConnection.conn.name
),
});
}

function BroadcastJoin(pSrcInfo: ConnInfo) {
const { id: _SenderId, conn: _SenderConn } = pSrcInfo;
const { name: _SenderName } = _SenderConn;
Expand Down