Laporan Akhir CSC
Laporan Akhir CSC
Disusun Oleh:
TEKNIK INFORMATIKA
2020/2021
A. General Explanation
With the rapid advances in the technology the network communication
between different people has made much easier than one can expect. And the network
is the heart of communication. Socket Application program interface is the main
utilization and which plays important base for this purpose. The current application
allows multiple users to communicate with each other which is enhanced to allow the
socket application program interface. That’s make communication mechanism makes
much easier. The application will allow different type of users to have communication
mechanism by implementing a simple client to server communication for mutual
chatting application. The objective of the chat server is to transfer messages reliably
and efficiently.
before we use this application, we must enter a username as identification.
then select the available room and enter the room to start communication between
users. if you want to leave the room there is a button for that.
B. Table Protocol
No. Pengirim Penerima Pesan Subyek, Deskripsi, dan Aksi
1 C S Join Room C: Mengirim data username dan
room
C. Application Screenshots
D. Code
i) Client
const chatMessageWrapper = document.querySelector('.chat-
messages');
const chatForm = document.getElementById('chat-form');
const roomNameElement = document.getElementById('room-name');
const usersListWrapper = document.getElementById('users');
const userTypingWrapper = document.querySelector('.typing-box');
const messageInput = document.getElementById('msg');
scrollScreenToNewMessage();
});
messageWrapper.appendChild(senderInfoElement);
messageWrapper.appendChild(messageContentElement);
chatMessageWrapper.appendChild(messageWrapper);
}
function displayUserTyping(username) {
userTypingWrapper.innerText = `${username} is typing...`;
}
function removeUserTypingDisplay() {
userTypingWrapper.innerText = ``;
}
return element;
}
function scrollScreenToNewMessage() {
chatMessageWrapper.scrollTop = chatMessageWrapper.scrollHeight;
}
function getUserDetails() {
const params = window.location.search;
const queries = new URLSearchParams(params);
console.log(username, room);
function setRoomName(roomName) {
roomNameElement.innerText = roomName;
}
function showUsersInRoom(listOfUsers) {
usersListWrapper.innerHTML = '';
listOfUsers.map((user) => {
const listElement = document.createElement('li');
listElement.innerText = user.username;
usersListWrapper.appendChild(listElement);
});
}
clearFormInput(event);
});
function sendMessage(event) {
const userMessage = event.target.elements.msg.value;
userMessage.trim();
socket.emit('message', userMessage);
return true;
}
function clearFormInput(event) {
event.target.elements.msg.value = '';
event.target.elements.msg.focus();
}
ii) Server
const express = require('express');
const socketio = require('socket.io');
const path = require('path');
const http = require('http');
const formatMessage = require('./utils/formatMessage.js');
const {
assignUserData,
getCurrentUser,
getLeavingUserAndUpdateUsersArray,
getUsersList,
} = require('./utils/usersHandlers.js');
app.use(express.static(path.join(__dirname, 'public')));
socket.join(user.room);
io.to(user.room).emit('message', formatMessage(user.username, u
serMessage));
});
socket.on('typing', () => {
const user = getCurrentUser(socket.id);
if (user) {
socket.to(user.room).broadcast.emit('typing', user);
}
});
socket.on('disconnect', () => {
const user = getLeavingUserAndUpdateUsersArray(socket.id)[0];
if (user) {
io.to(user.room).emit('leave room', getUsersList(user.room));
io.to(user.room).emit('message', formatMessage(adminName, `${
user.username} has left the room`));
}
});
});