1.1.6 • Published 4 years ago

wsec v1.1.6

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

wsec

Nodejs websocket. Easy and customize-able and fast

install

npm install wsec --save

use

methods 1

will handle only ws

const wsec = require('wsec');

const options = {
    host: 'localhost',
    port: 8080
}

const handler = (socket) => {
    // each connection params contain header information and write methods
    socket.on('connected', (connection) => {
        console.log('connected new user');
        // connection.send("congras to new user"); //to write or send text to connected user
    });
    socket.on('data', (connection, data) => {
        console.log(data);
    });
    socket.on('end', (connection) => {
        console.log("connection closed");
    });
}

new wsec(options, handler);

methods 2

Use with your Nodejs HTTP or HTTPS server

const wsec = require('wsec');

const options = {noServer: true};

const handler = (socket) => {
    // each connection params contain header information and write methods
    socket.on('connected', (connection) => {
        console.log('connected new user');
        // connection.send("congras to new user"); //to write or send text to connected user
    });
    socket.on('data', (connection, data) => {
        console.log(data);
        connection.send(JSON.stringify({status: true}));
    });
    socket.on('end', (connection) => {
        console.log("connection closed");
    });
}

const ws = new wsec(options, handler)

const server = http.createServer((req, res) => {
    if(req.url === '/socket') {
        ws.handleHttpServerConnection(req);
    } else {
        // handle others route
    }
});

server.listen(8080, () => {
    console.log("Server start at 8080");
});

Client Side

This library need no client side library. You can use native methods in Client side

const socketIns = new WebSocket('ws://localhost:8080/socket');
socketIns.onmessage = function (msg) { // function will fire when data is received
    console.log(msg.data);
    socketIns.send("Hello server"); // send data to server
}

Send large file

function fileUpload() {
    const img = document.createElement('INPUT');
    img.setAttribute("type", "file"); 
    img.onchange = () => {
        this.readFile(img.files[0]);
    };
    img.click();
}

function readFile(file) {
    const reader = new FileReader();
    reader.onload = e => {
        this.sendFile(e.target.result);
    };
    reader.readAsDataURL(file);
}

function sendFile(data) {
    socketIns.send(data);
}

fileUpload();

Utility

Save identifier for each request

const wsec = require('wsec');

const options = {
    host: 'localhost',
    port: 8080
}

const handler = (socket) => {
    socket.on('connected', (connection) => {
        connection.setState( // set some data to per connection
            {
                id: 'xxxx',
                name: 'AM'
            }
        );
    });
    socket.on('data', (connection, data) => {
        if(connection.state.id === 'xxx') { // get this data
            // Do something
        } else {
            // Do else something
        }
    });
}

new wsec(options, handler);
1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago