|
@@ -32,6 +32,7 @@ export class SocketService {
|
|
|
public async setUpConnection() {
|
|
|
this.io.on('connection', (socket) => {
|
|
|
this.announcements.next('a client is connected:' + socket.id);
|
|
|
+ let clientIsOnline: BehaviorSubject<boolean> = new BehaviorSubject(true)
|
|
|
let clientInfo: ClientInfo | null
|
|
|
|
|
|
socket.on('connect', (msg) => {
|
|
@@ -40,7 +41,71 @@ export class SocketService {
|
|
|
|
|
|
socket.on('notification', (msg) => {
|
|
|
console.log(msg)
|
|
|
- clientInfo = this.handleNotification(msg, socket, clientInfo)
|
|
|
+ if (msg.agenda == 'newClient') {
|
|
|
+ clientInfo = {
|
|
|
+ id: socket.id,
|
|
|
+ clientName: uuidV4(),
|
|
|
+ connectedAt: new Date(),
|
|
|
+ clientConnectionState: new BehaviorSubject<'ONLINE' | 'OFFLINE'>('ONLINE'),
|
|
|
+ requests: [],
|
|
|
+ buffer: new RetransmissionService(),
|
|
|
+ responseObs: new Subject<BaseMessage>()
|
|
|
+ }
|
|
|
+ this.connectedClients.push(clientInfo);
|
|
|
+
|
|
|
+ // Send data over for client to persist
|
|
|
+ socket.emit('notification', {
|
|
|
+ notification: 'Your credentials',
|
|
|
+ createdAt: new Date(),
|
|
|
+ socketInfo: clientInfo
|
|
|
+ })
|
|
|
+
|
|
|
+ // this is the supposed responses to be pushed to this socket client
|
|
|
+ clientInfo.buffer.retransmission(clientInfo.responseObs, clientInfo.clientConnectionState)
|
|
|
+ let subscription = clientInfo.buffer.returnBufferedMessages().subscribe(output => {
|
|
|
+ // console.log(output)
|
|
|
+ if (clientIsOnline.getValue() === true) {
|
|
|
+ socket.emit('response', output)
|
|
|
+ } else {
|
|
|
+ subscription.unsubscribe()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msg.agenda == 'existingClient') {
|
|
|
+ // check if client exists
|
|
|
+ let clientObj = this.connectedClients.find(obj => obj.clientName === msg.data.clientName)
|
|
|
+ if (clientObj) {
|
|
|
+ // clientInfo = clientObj
|
|
|
+ console.log('Existing client found')
|
|
|
+ // but also update socketId
|
|
|
+ clientObj.id = socket.id
|
|
|
+
|
|
|
+ // Send data over for client to persist
|
|
|
+ socket.emit('notification', {
|
|
|
+ notification: 'Your updated credentials',
|
|
|
+ connectedAt: new Date(),
|
|
|
+ updatedId: socket.id
|
|
|
+ })
|
|
|
+
|
|
|
+ socket.emit('notification', `Hello from server. You have been assigned ${socket.id}`);
|
|
|
+ // resume operation Release them buffer
|
|
|
+ /* local client isOnline need not be mutated, since this is a new connection. However the previous intance of client Connection State
|
|
|
+ inside the retransmission needs to be updated to release the buffered values.*/
|
|
|
+ clientObj.clientConnectionState.next('ONLINE')
|
|
|
+ let subscription = clientObj.buffer.returnBufferedMessages().subscribe(output => {
|
|
|
+ // console.log(output)
|
|
|
+ if (clientIsOnline.getValue() === true) {
|
|
|
+ socket.emit('response', output)
|
|
|
+ } else {
|
|
|
+ subscription.unsubscribe()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ console.log(this.connectedClients)
|
|
|
+ console.log(`Existing Client is not found`)
|
|
|
+ }
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
// Listen for messages from the client
|
|
@@ -70,7 +135,8 @@ export class SocketService {
|
|
|
// Handle disconnection
|
|
|
socket.on('disconnect', () => {
|
|
|
if (clientInfo) {
|
|
|
- clientInfo.clientConnectionState.next('OFFLINE') // signal to start buffering
|
|
|
+ clientIsOnline.next(false)
|
|
|
+ clientInfo.clientConnectionState.next('OFFLINE') // signal to start buffering\
|
|
|
this.announcements.next(`Client ${clientInfo.id} disconnected`);
|
|
|
// this.deleteClientById(socket.id)
|
|
|
}
|
|
@@ -118,60 +184,6 @@ export class SocketService {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- private handleNotification(msg: any, socket: Socket, clientInfo: ClientInfo | null) {
|
|
|
- if (msg.agenda == 'newClient') {
|
|
|
- clientInfo = {
|
|
|
- id: socket.id,
|
|
|
- clientName: uuidV4(),
|
|
|
- connectedAt: new Date(),
|
|
|
- clientConnectionState: new BehaviorSubject<'ONLINE' | 'OFFLINE'>('ONLINE'),
|
|
|
- requests: [],
|
|
|
- buffer: new RetransmissionService(),
|
|
|
- responseObs: new Subject<BaseMessage>()
|
|
|
- }
|
|
|
- this.connectedClients.push(clientInfo);
|
|
|
-
|
|
|
- // Send data over for client to persist
|
|
|
- socket.emit('notification', {
|
|
|
- notification: 'Your credentials',
|
|
|
- createdAt: new Date(),
|
|
|
- socketInfo: clientInfo
|
|
|
- })
|
|
|
-
|
|
|
- // this is the supposed responses to be pushed to this socket client
|
|
|
- clientInfo.buffer.retransmission(clientInfo.responseObs, clientInfo.clientConnectionState).subscribe(output => {
|
|
|
- // console.log(output)
|
|
|
- socket.emit('response', output)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- if (msg.agenda == 'existingClient') {
|
|
|
- // check if client exists
|
|
|
- let clientObj = this.connectedClients.find(obj => obj.clientName === msg.data.clientName)
|
|
|
- if (clientObj) {
|
|
|
- clientInfo = clientObj
|
|
|
- console.log('Existing client found')
|
|
|
- // but also update socketId
|
|
|
- clientObj.id = socket.id
|
|
|
-
|
|
|
- // Send data over for client to persist
|
|
|
- socket.emit('notification', {
|
|
|
- notification: 'Your updated credentials',
|
|
|
- connectedAt: new Date(),
|
|
|
- socketInfo: clientInfo
|
|
|
- })
|
|
|
-
|
|
|
- socket.emit('notification', `Hello from server. You have been assigned ${socket.id}`);
|
|
|
- // resume operation
|
|
|
- clientObj.clientConnectionState.next('ONLINE')
|
|
|
- } else {
|
|
|
- console.log(this.connectedClients)
|
|
|
- console.log(`Existing Client is not found`)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return clientInfo
|
|
|
- }
|
|
|
|
|
|
}
|
|
|
|