Nodejs websocket. Easy and customize-able and fast
npm install wsec --save
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);
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");
});
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
}
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();
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);