Coder Social home page Coder Social logo

client-secure-chat's Introduction

Một ứng dụng chat an toàn sử dụng react va signal protocol

Công nghệ sử dụng

  1. Thu vien ReactJs de tao giao dien
  2. Giao thuc Signal cho ma hoa dau cuoi
  3. Axios cho AJAX
  4. LocalStorage de luu cac message
  5. Web Sockets cho viec nhan tin tuc thi

Components

  1. Login
  2. Chat Window
    1. Contact List: Danh sach ban be de nhan tin
    2. Message Box : Luu tru cac tin nhan va hien thi chung

Axios Calls

  1. GET - api/users/login/userName/password - Tra ve doi tuong User
  2. GET - api/users/users/userId/role - Tra ve nguoi dung co vai tro khac voi nguoi dung hien tai

Web Sockets

  1. Khoi tao mot ket noi WS: let webSocket = new WebSocket("ws://localhost:3000/chat")
  2. Lang nghe su kien cua doi tuong Socket:
    webSocket.onopen = () => {
        console.log(‘WebSocket Client Connected’);
        webSocket.send('Hi this is web client.');
    };
    webSocket.onmessage = (e) => {
        console.log(‘Received: ’ + e.data);
    };
    webSocket.close = () => {
        console.log('WebSocket Client Closed.’);
    };

Giao thức trao đổi tin nhắn an toàn Signal

  1. InMemorySignalProtocolStore.js (and helpers.js) được lấy cho mục đích lưu trữ từ Signal Github (liên kết được đề cập trong tài nguyên)
  2. libsignal-protocol.js (cũng từ Signal Github) triển khai giao thức
  3. Signal Gateway - Được tạo ra để tích hợp React với Signal. Nó thực hiện chức năng Khởi tạo, Mã hóa và Giải mã khi được yêu cầu trên Frontend.
  4. Các method cho việc mã hoá:
async getNewMsgObj(newMsgObj) {
        let selectedUserChatId = this.getSelectedUserChatId()
        let msgToSend = { chatId: selectedUserChatId, senderid: this.props.loggedInUserObj._id, receiverid: this.state.messageToUser._id, ...newMsgObj }
        //Gửi tin nhắn mã hóa đến Signal Protocol, sau đó gửi tin nhắn mã hóa đến máy chủ chính
        try {
            let encryptedMessage = await this.props.signalProtocolManagerUser.encryptMessageAsync(this.state.messageToUser._id, newMsgObj.message);
            msgToSend.message = encryptedMessage
            this.state.ws.send(JSON.stringify(msgToSend))
            this.setState({ lastSentMessage: newMsgObj.message }) // Storing last-sent message for Verification with Received Message
        } catch (error) {
            console.log(error);
        }
    }
  1. Các method cho việc giải mã:
ws.onmessage = async (e) => {
            let newMessage = JSON.parse(e.data)
            //Xác minh những tin nhắn được nhận thành công
            if (newMessage.senderid === this.props.loggedInUserObj._id) {
                newMessage.message = this.state.lastSentMessage
            } else { // Trương hợp khác mã hoá nó và lưu nó vào newMessage
                // Mã hoá sử dụng giao thức Signal
                let decrytedMessage = await this.props.signalProtocolManagerUser.decryptMessageAsync(newMessage.senderid, newMessage.message)
                newMessage.message = decrytedMessage
            }
    }

Nguồn

  1. Signal Protocol in JavaScript Github
  2. Why Axios
  3. ReactJS
  4. Web Sockets API

client-secure-chat's People

Contributors

ddotu2612 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.