Commit fa9d823f authored by Dennis Willers's avatar Dennis Willers 🏀

Add Socketserver

parent d0100164
Pipeline #411 passed with stages
in 2 minutes and 48 seconds
const Express = require("express")();
const Http = require("http").Server(Express);
const Socketio = require("socket.io")(Http, {
cors: {
origin: '*',
}
});
var net = require('net');
// creates the server
var server = net.createServer();
Http.listen(3000, () => {
console.log("Listening at :3000...");
//emitted when server closes ...not emitted until all connections closes.
server.on('close',function(){
console.log('Server closed !');
});
Socketio.on("connection", socket => {
console.log('new Socket connect: ', socket.id);
socket.on("newGame", data => {
print("New Game");
// emitted when new client connects
server.on('connection',function(socket){
//this property shows the number of characters currently buffered to be written. (Number of characters is approximately equal to the number of bytes to be written, but the buffer may contain strings, and the strings are lazily encoded, so the exact number of bytes is not known.)
//Users who experience large or growing bufferSize should attempt to "throttle" the data flows in their program with pause() and resume().
console.log('Buffer size : ' + socket.bufferSize);
console.log('---------server details -----------------');
var address = server.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Server is listening at port' + port);
console.log('Server ip :' + ipaddr);
console.log('Server is IP4/IP6 : ' + family);
var lport = socket.localPort;
var laddr = socket.localAddress;
console.log('Server is listening at LOCAL port' + lport);
console.log('Server LOCAL ip :' + laddr);
console.log('------------remote client info --------------');
var rport = socket.remotePort;
var raddr = socket.remoteAddress;
var rfamily = socket.remoteFamily;
console.log('REMOTE Socket is listening at port' + rport);
console.log('REMOTE Socket ip :' + raddr);
console.log('REMOTE Socket is IP4/IP6 : ' + rfamily);
console.log('--------------------------------------------')
//var no_of_connections = server.getConnections(); // sychronous version
server.getConnections(function(error,count){
console.log('Number of concurrent connections to the server : ' + count);
});
socket.setEncoding('utf8');
socket.setTimeout(800000,function(){
// called after timeout -> same as socket.on('timeout')
// it just tells that soket timed out => its ur job to end or destroy the socket.
// socket.end() vs socket.destroy() => end allows us to send final data and allows some i/o activity to finish before destroying the socket
// whereas destroy kills the socket immediately irrespective of whether any i/o operation is goin on or not...force destry takes place
console.log('Socket timed out');
});
socket.on('map', function (data) {
console.log('MAP Client Connect to: ', data);
});
socket.on("disconnect", () => {
console.log("DISCONNECT CLIENT: ", socket.id);
socket.on('data',function(data){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Data sent to server : ' + data);
//echo data
var is_kernel_buffer_full = socket.write('FROMSERVER: ' + data);
if(is_kernel_buffer_full){
console.log('Data was flushed successfully from kernel buffer i.e written successfully!');
}else{
socket.pause();
}
});
socket.on('drain',function(){
console.log('write buffer is empty now .. u can resume the writable stream');
socket.resume();
});
socket.on('error',function(error){
console.log('Error : ' + error);
});
socket.on('timeout',function(){
console.log('Socket timed out !');
socket.end('Timed out!');
// can call socket.destroy() here too.
});
socket.on('end',function(data){
console.log('Socket ended from other end!');
console.log('End data : ' + data);
});
socket.on('close',function(error){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Socket closed!');
if(error){
console.log('Socket was closed coz of transmission error');
}
});
setTimeout(function(){
var isdestroyed = socket.destroyed;
console.log('Socket destroyed:' + isdestroyed);
socket.destroy();
},1200000);
});
// emits when any error occurs -> calls closed event immediately after this.
server.on('error',function(error){
console.log('Error: ' + error);
});
//emits when server is bound with server.listen
server.on('listening',function(){
console.log('Server is listening!');
});
server.maxConnections = 500;
//static port allocation
server.listen(3000);
// for dyanmic port allocation
/*server.listen(function(){
var address = server.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Server is listening at port' + port);
console.log('Server ip :' + ipaddr);
console.log('Server is IP4/IP6 : ' + family);
});*/
var islistening = server.listening;
if(islistening){
console.log('Server is listening');
}else{
console.log('Server is not listening');
}
setTimeout(function(){
server.close();
},5000000);
//---------------------client----------------------
// creating a custom socket client and connecting it....
/*var client = new net.Socket();
client.connect({
port:3000
});
client.on('connect',function(){
console.log('Client: connection established with server');
console.log('---------client details -----------------');
var address = client.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Client is listening at port' + port);
console.log('Client ip :' + ipaddr);
console.log('Client is IP4/IP6 : ' + family);
// writing data to server
client.write('hello from client');
});
client.setEncoding('utf8');
client.on('data',function(data){
console.log('Data from server:' + data);
});
/*setTimeout(function(){
client.end('Bye bye server');
},5000);
//NOTE:--> all the events of the socket are applicable here..in client...
// -----------------creating client using net.connect instead of custom socket-------
// server creation using net.connect --->
// u can also => write the below code in seperate js file
// open new node instance => and run it...
const clients = net.connect({port: 3000}, () => {
// 'connect' listener
console.log('connected to server!');
clients.write('world!\r\n');
});
clients.on('data', (data) => {
console.log(data.toString());
clients.end();
});
clients.on('end', () => {
console.log('disconnected from server');
});
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment