Skip to content

Commit

Permalink
Remove acknowledgement
Browse files Browse the repository at this point in the history
  • Loading branch information
HeidiTran committed Jul 22, 2020
1 parent 0cc9f4c commit a35ac92
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 75 deletions.
4 changes: 2 additions & 2 deletions rapid-chat-client/src/app/chat/chat.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
[formGroup]="messageForm"
(ngSubmit)="sendMessage(messageForm.value)">
<div class="form-group d-flex">
<button class="btn btn-light" (click)="toggleEmojiPicker()">
<a class="btn btn-light" (click)="toggleEmojiPicker()">
<i class="fa fa-smile-o fa-lg"></i>
</button>
</a>
<input type="text"
formControlName="messageToSend"
class="form-control"
Expand Down
1 change: 1 addition & 0 deletions rapid-chat-client/src/app/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ export class ChatComponent implements OnInit {
this.messages = this.peerService.getAllMessages();
this.messageForm.setValue({ messageToSend: "" });
setTimeout(() => window.scrollTo(0, 1000000), 10); // Wait 10 milli sec for message to be updated
this.showEmojiPicker = false;
}
}
75 changes: 3 additions & 72 deletions rapid-chat-client/src/app/services/peer.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { EventEmitter, Injectable } from "@angular/core";
import { Message, MessageType } from "../shared/Message";
import { RoomService } from "./room.service";
import { Router } from "@angular/router";

declare const Peer: any;

@Injectable({
providedIn: "root",
})
export class PeerService {
private timeWaitForAck = 10000; // Millisecond
private time = 0;
private peer: any;
private roomName: string;
Expand All @@ -18,12 +16,11 @@ export class PeerService {
private peerIdsToSendOldChatMessages: string[] = [];
private connectionsIAmHolding: any[] = [];
private previousMessages: Message[] = [];
private messagesToBeAcknowledged: Message[] = [];
private hasReceivedAllMessages = false;
connectionEstablished = new EventEmitter<Boolean>();
infoBroadcasted = new EventEmitter<any>();

constructor(private roomService: RoomService, private router: Router) {
constructor(private roomService: RoomService) {
// Create a new peer and connect to peerServer. We can get our id from this.peer.id
this.peer = new Peer({
host: "dinamopeerserver.herokuapp.com/",
Expand Down Expand Up @@ -119,24 +116,12 @@ export class PeerService {
case MessageType.Message:
this.addUniqueMessages([message], this.previousMessages);
this.infoBroadcasted.emit(BroadcastInfo.UpdateAllMessages);
// Send Acknowledgement
fromConn.send(
JSON.stringify(
new Message(null, MessageType.Acknowledge, null, null, message.time)
)
);
break;
case MessageType.AllMessages:
this.hasReceivedAllMessages = true;
const messages: Message[] = JSON.parse(message.content);
this.addUniqueMessages(messages, this.previousMessages);
this.infoBroadcasted.emit(BroadcastInfo.UpdateAllMessages);
// Send Acknowledgement
fromConn.send(
JSON.stringify(
new Message(null, MessageType.Acknowledge, null, null, message.time)
)
);
this.connectToTheRestInRoom(fromConn.peer);
break;
case MessageType.RequestAllMessages:
Expand All @@ -152,14 +137,6 @@ export class PeerService {
}
}
break;
case MessageType.Acknowledge:
const indexDelete = this.messagesToBeAcknowledged.findIndex(
(mes) => mes.time === message.time
);
if (indexDelete !== -1) {
this.messagesToBeAcknowledged.splice(indexDelete, 1);
}
break;
default:
throw new Error("Unhandled message type");
}
Expand Down Expand Up @@ -221,11 +198,6 @@ export class PeerService {
this.time++
);
conn.send(JSON.stringify(message));
this.messagesToBeAcknowledged.push(message);
const that = this; // setTimeOut will not know what 'this' is => Store 'this' in a variable
setTimeout(function () {
that.acknowledgeOrResend(message);
}, that.timeWaitForAck);
}
//*************************************************************

Expand Down Expand Up @@ -258,7 +230,7 @@ export class PeerService {
createNewRoom() {
this.roomService.joinNewRoom(this.peer.id).subscribe((data: string) => {
this.roomName = data;
console.log("roomName: " + this.roomName);

// No peerId
this.handleFirstJoinRoom([]);
this.infoBroadcasted.emit(BroadcastInfo.RoomName);
Expand All @@ -271,10 +243,8 @@ export class PeerService {
(peerIds) => {
console.log(peerIds);
if (peerIds.length === 1 && peerIds[0] === "ROOM_NOT_EXIST") {
// Either room not exists or has been deleted

window.location.replace("/");
alert("Room not exists, navigating back to home");
window.location.replace("/");
}
this.handleFirstJoinRoom(peerIds);
},
Expand Down Expand Up @@ -303,45 +273,10 @@ export class PeerService {
);
const messageInJson = JSON.stringify(messageToSend);
conn.send(messageInJson);
this.messagesToBeAcknowledged.push(messageToSend);
const that = this; // setTimeOut will not know what 'this' is => Store 'this' in a variable
setTimeout(function () {
that.acknowledgeOrResend(messageToSend);
}, that.timeWaitForAck);
});
this.time++;
}

acknowledgeOrResend(mess: Message, hasSent = 0) {
// If message hasn't been received
if (
this.messagesToBeAcknowledged.find(
(message) => message.time === mess.time
)
) {
const conn = this.connectionsIAmHolding.find(
(connection) => connection.peer === mess.toPeerId
);
// Has sent for more than 5 times
if (hasSent > 5) {
this.connectionsIAmHolding = this.connectionsIAmHolding.filter(
(connection) => connection.peer !== conn.peer
);
return;
}

// If that peer hasn't disconnect
if (conn) {
conn.send(JSON.stringify(mess));
console.log("Waiting too long for ack. Resent messages");
const that = this; // setTimeOut will not know what 'this' is => Store 'this' in a variable
setTimeout(function () {
that.acknowledgeOrResend(mess, hasSent + 1);
}, that.timeWaitForAck);
}
}
}

hasReceivedMessage(message: Message): boolean {
return (
this.previousMessages.find(
Expand All @@ -366,10 +301,6 @@ export class PeerService {
getAllPeerIds(): string[] {
return this.connectionsIAmHolding.map((conn) => conn.peer);
}

getMessagesToBeAck(): any[] {
return this.messagesToBeAcknowledged;
}
}

export const enum PeerEvent {
Expand Down
1 change: 0 additions & 1 deletion rapid-chat-client/src/app/shared/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ export const enum MessageType {
Message = 0,
AllMessages = 1,
RequestAllMessages = 2,
Acknowledge = 3,
}
Binary file modified rapidchat.db
Binary file not shown.

0 comments on commit a35ac92

Please sign in to comment.