OntarioTech-CS-program / w23-csci2020u-project-team09

w23-csci2020u-project-team9 created by GitHub Classroom
0 stars 0 forks source link

Emojis (Sending & Receiving) via WebSockets #7

Open smutharasan opened 1 year ago

smutharasan commented 1 year ago

You need to implement two new functions:

signature of the OnMessage function

 @OnMessage
public void onMessage(byte[] data,boolean last, Session session) {

 }

Code template for the client side

Normally we use websocket.send(blah) to the websocket server. But now it will be as follows:


SendFile(fileMeta, fileData)
{
    const fileMetaJson = JSON.stringify({
        lastModified : fileMeta.lastModified,
        name         : fileMeta.name,
        size         : fileMeta.size,
        type         : fileMeta.type,
    });

    // _must_ do this to encode as a ArrayBuffer / Uint8Array
    const enc  = new TextEncoder(); // always utf-8, Uint8Array()
    const buf1 = enc.encode('!');
    const buf2 = enc.encode(fileMetaJson);
    const buf3 = enc.encode("\r\n\r\n");
    const buf4 = fileData;

    let sendData = new Uint8Array(buf1.byteLength + buf2.byteLength + buf3.byteLength + buf4.byteLength);
    sendData.set(new Uint8Array(buf1), 0);
    sendData.set(new Uint8Array(buf2), buf1.byteLength);
    sendData.set(new Uint8Array(buf3), buf1.byteLength + buf2.byteLength);
    sendData.set(new Uint8Array(buf4), buf1.byteLength + buf2.byteLength + buf3.byteLength);

    this.conn.binaryType = "arraybuffer";
    this.conn.send(sendData);
    this.conn.binaryType = "blob";
}

https://hackage.haskell.org/package/websockets-0.12.4.1/docs/Network-WebSockets-Connection.html#v:sendBinaryData

----- Some other links

https://stackoverflow.com/questions/60531412/how-to-send-an-image-from-server-to-client-with-web-socket-using-java-javascri

https://www.dynamsoft.com/codepool/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html

pseudocode: