Skip to content

Commit

Permalink
Added friend commands
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebires committed Mar 21, 2022
1 parent 8367e03 commit d6065d9
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 39 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ LIME - A lightweight messaging library

![Go](https://github.com/takenet/lime-go/workflows/Go/badge.svg?branch=master)

LIME allows you to build scalable, real-time messaging applications using a JSON-based [open protocol](http://limeprotocol.org). It's **fully asynchronous** and supports any persistent transport like TCP or Websockets.
LIME allows you to build scalable, real-time messaging applications using a JSON-based [open protocol](http://limeprotocol.org).
It's **fully asynchronous** and support persistent transports like TCP or Websockets.

You can send and receive any type of document into the wire as long it can be represented as JSON or text (plain or encoded with base64) and it has a **MIME type** to allow the other party handle it in the right way.

The connected nodes can send receipts to the other parties to notify events about messages (for instance, a message was received or the content invalid or not supported).

Besides that, there's a **REST capable** command interface with verbs (*get, set and delete*) and resource identifiers (URIs) to allow rich messaging scenarios. You can use that to provide services like on-band account registration or instance-messaging resources, like presence or roster management.
Besides that, there's a **REST capable** command interface with verbs (*get, set and delete*) and resource identifiers (URIs) to allow rich messaging scenarios.
You can use that to provide services like on-band account registration or instance-messaging resources, like presence or roster management.

Finally, it has built-in support for authentication, transport encryption and compression.

Expand Down
22 changes: 12 additions & 10 deletions examples/ws-chat/client/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

<main class="msger-chat">
<div class="msg left-msg">
<div
class="msg-img"
style="background-image: url(../images/bot.png)"
></div>
<div class="msg-img" style="background-image: url(../images/bot.png)"></div>

<div class="msg-bubble">
<div class="msg-info">
Expand All @@ -31,19 +28,24 @@
The available commands are:<br><br>
<ul>
<li>
<strong>/name [new_nickname]</strong>: Sets a new nickname
<strong>/name [new_nickname]</strong>: Sets a new nickname.
</li>
<li>
<strong>/to [nickname] [message]</strong>: Sends a private message to a nickname
<strong>/to [nickname] [message]</strong>: Sends a private message to a nickname.
</li>
<li>
<strong>/friends</strong>: Display your friends list.
</li>
<li>
<strong>/add [nickname]</strong>: Adds a nickname to your friends list.
</li>
<li>
<strong>/remove [nickname]</strong>: Removes a nickname from your friends list.
</li>
</ul>
</div>
</div>
</div>

<div class="msg right-msg">

</div>
</main>

<form class="msger-inputarea">
Expand Down
122 changes: 104 additions & 18 deletions examples/ws-chat/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const BOT_IMG = "../images/bot.png";

let nickname = `guest`;
let client = await createClient();
let closing = false;

// Notify other users
await client.sendMessage({
Expand All @@ -30,6 +31,11 @@ async function createClient() {
client.transport.onError = async function () {
client = await connect();
}
client.transport.onClose = async function() {
if (!closing) {
client = await connect();
}
}
client.onMessage = (message) => {
switch (message.type) {
case 'application/x-chat-nickname+json':
Expand Down Expand Up @@ -61,8 +67,10 @@ msgerForm.addEventListener("submit", async event => {
const msgText = msgerInput.value;
if (!msgText) return;

appendMessage(nickname, PERSON_IMG, "right", msgText);
msgerInput.value = "";

if (await parseCommand(msgText)) {
msgerInput.value = "";
return;
}

Expand All @@ -77,28 +85,47 @@ async function sendMessage(content, type, to = null) {
type: type ?? "text/plain",
content: content,
});

appendMessage(nickname, PERSON_IMG, "right", content);
msgerInput.value = "";
}



async function parseCommand(input) {
if (input.startsWith("/name ")) {
let arg = input.split(" ")[1];
if (arg) {
await setNickname(arg);
return true;
}
}
let args = input.split(' ');

switch (args[0]) {
case '/name':
if (args.length > 1) {
await setNickname(args[1]);
return true;
}
break;

case '/to':
if (args.length >= 2) {
let to = args[1];
let content = args.slice(2).join(' ');
await sendMessage(content, 'text/plain', to);
return true;
}
break;

if (input.startsWith("/to")) {
let args = input.split(" ");
if (args.length >= 2) {
let to = args[1];
let content = args.slice(2).join(" ");
await sendMessage(content, "text/plain", to);
case '/friends':
await getFriends();
return true;
}

case '/add':
if (args.length > 1) {
await addFriend(args[1]);
return true;
}
break;

case '/remove':
if (args.length > 1) {
await removeFriend(args[1]);
return true;
}
break;
}

return false;
Expand All @@ -108,9 +135,13 @@ async function setNickname(newNickname) {
let oldNickname = nickname;
nickname = newNickname;

closing = true;

await client.sendFinishingSession();
client = await createClient();

closing = false;

// Notify other users
await client.sendMessage({
id: uuidv4(),
Expand Down Expand Up @@ -146,6 +177,61 @@ function appendMessage(name, img, side, text) {
msgerChat.scrollTop += 500;
}

async function getFriends() {
let response = await client.processCommand({
id: uuidv4(),
method: 'get',
uri: '/friends'
});

if (response.status !== 'success') {
appendMessage("BOT", BOT_IMG, "left", `Ops, an error occurred while retrieving your friends list: ${response.reason.description}`);
return;
}

let responseMsg = 'Your friends are:<br>';
for (let i = 0; i < response.resource.items.length; i++) {
let friend = response.resource.items[i];
responseMsg += `- ${friend.nickname}${friend.online? ' (online)': '' } <br>`;
}

appendMessage("BOT", BOT_IMG, "left", responseMsg);
}

async function addFriend(nickname) {
let response = await client.processCommand({
id: uuidv4(),
method: 'set',
uri: '/friends',
type: 'application/x-chat-friend+json',
resource: {
nickname: nickname,
}
});

if (response.status !== 'success') {
appendMessage("BOT", BOT_IMG, "left", `Ops, an error occurred while adding this friends: ${response.reason.description}`);
return;
}

appendMessage("BOT", BOT_IMG, "left", `The nickname '${nickname}' was added to your friends list.`);
}

async function removeFriend(nickname) {
let response = await client.processCommand({
id: uuidv4(),
method: 'delete',
uri: `/friends/${nickname}`
});

if (response.status !== 'success') {
appendMessage("BOT", BOT_IMG, "left", `Ops, an error occurred while removing this friends: ${response.reason.description}`);
return;
}

appendMessage("BOT", BOT_IMG, "left", `The nickname '${nickname}' was removed from your friends list.`);
}

// Utils
function get(selector, root = document) {
return root.querySelector(selector);
Expand Down
Loading

0 comments on commit d6065d9

Please sign in to comment.