Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ws.on('close', function() {
if (++ssl_disconnections == 519) {
ssl.close();
}
});
});
httpsServer.listen(3001);
/**
* HTTP module.
*/
const document: Buffer = Buffer.from('Hello world!');
const server: http.Server = WebSocket.http.createServer(
(req: http.IncomingMessage, res: http.ServerResponse): void => {
if (req.method === 'POST') {
const body: Buffer[] = [];
req.on('data', (chunk: Buffer) => {
body.push(Buffer.from(chunk));
}).on('end', () => {
res.end('You posted me this: ' + Buffer.concat(body).toString());
});
// handle some GET url
} else if (req.url === '/') {
res.end(document);
} else {
res.end('Unknown request by: ' + req.headers['user-agent']);
}
});
ws.on('close', function() {
if (++ssl_disconnections == 519) {
ssl.close();
}
});
});
httpsServer.listen(3001);
/**
* HTTP module.
*/
const document: Buffer = Buffer.from('Hello world!');
const server: http.Server = WebSocket.http.createServer(
(req: http.IncomingMessage, res: http.ServerResponse): void => {
if (req.method === 'POST') {
const body: Buffer[] = [];
req.on('data', (chunk: Buffer) => {
body.push(Buffer.from(chunk));
}).on('end', () => {
res.end('You posted me this: ' + Buffer.concat(body).toString());
});
// handle some GET url
} else if (req.url === '/') {
res.end(document);
} else {
res.end('Unknown request by: ' + req.headers['user-agent']);
}
});
//app.use(morgan('dev'));
app.use(cors({
exposedHeaders: "*"
}));
app.use(bodyParser.json({
limit: '50mb'
}));
app.wss = new Server({
server: app.server
});
// static www files use express
const wwwPath = path.join(__dirname, 'www');
app.use('/', express.static(wwwPath));
// Connect to Mongo Database
new Database().connect().then((db) => {
console.log("Successful connected to database.")
app.db = db;
Gun.on('opt', function mount(ctx){
this.to.next(ctx);
var opt = ctx.opt;
if(ctx.once){ return }
if(!opt.web){ return }
var ws = opt.uws || opt.ws || (opt.uws = {}), batch;
ws.server = ws.server || opt.web;
ws.path = ws.path || '/gun';
ws.web = new WebSocket.Server(ws);
ws.web.on('connection', function(peer){
peer.upgradeReq = peer.upgradeReq || {};
peer.url = url.parse(peer.upgradeReq.url||'', true);
peer.id = peer.id || Gun.text.random(6);
opt.peers[peer.id] = {wire: peer};
peer.on('message', function(msg){
//console.log("MESSAGE", msg);
receive(msg, peer, ctx); // diff: peer is wire.
});
peer.on('close', function(){
Gun.obj.del(opt.peers, peer.id);
});
});
ctx.on('out', function(at){
* @param {string} type Type of the message.
* @param {*} [payload] Payload of the message.
* @param {ServerSideSocket[]} [sockets] Sockets to broadcast the message
* between.
*/
this.socketExtensions.set('broadcast', (socket) =>
(type, payload, sockets = this.sockets) => {
this.bulkSend(
[...sockets].filter((socket2) => socket2 !== socket),
type,
payload
);
}
);
this.base = new WebSocketServer(options, successCallback)
.on('error', (error) => this.emit('error', error))
.on('connection', (socket) => {
// Extend the functionality of sockets
this.socketExtensions.apply(socket, this);
// Add the connected socket to the main group of sockets
this.sockets.add(socket);
socket
.on('error', (error) => this.emit('error', error, socket))
.on('close', (code, reason) => {
// Remove the disconnected socket from every group
this.sockets.delete(socket);
for (const socketGroup of Object.values(this.socketGroups)) {
socketGroup.delete(socket);
}
'use strict';
const http = require('http');
const url = require('url');
const uws = require('uws');
const native = uws.native;
//
// uws v0.12.0+ needs a set "no operation" callback.
//
if (native.setNoop) native.setNoop(() => {});
/**
* Server of µWebSockets transformer.
*
* @runat server
* @api private
*/
module.exports = function server() {
const opts = Object.assign({
perMessageDeflate: !!this.primus.options.compression,
maxPayload: this.primus.options.maxLength
module.exports = function server() {
const opts = Object.assign({
perMessageDeflate: !!this.primus.options.compression,
maxPayload: this.primus.options.maxLength
}, this.primus.options.transport);
this.service = native.server;
let flags = 0;
if (opts.perMessageDeflate) {
flags |= uws.PERMESSAGE_DEFLATE;
if (opts.perMessageDeflate.serverNoContextTakeover === false) {
flags |= uws.SLIDING_DEFLATE_WINDOW;
}
}
const group = native.server.group.create(flags, opts.maxPayload);
let upgradeReq = null;
native.server.group.onConnection(group, (socket) => {
const spark = new this.Spark(
upgradeReq.headers, // HTTP request headers.
upgradeReq, // IP address location.
url.parse(upgradeReq.url).query, // Optional query string.
null, // We don't have an unique id.
upgradeReq, // Reference to the HTTP req.
socket // Reference to the socket.
module.exports = function server() {
const opts = Object.assign({
perMessageDeflate: !!this.primus.options.compression,
maxPayload: this.primus.options.maxLength
}, this.primus.options.transport);
this.service = native.server;
let flags = 0;
if (opts.perMessageDeflate) {
flags |= uws.PERMESSAGE_DEFLATE;
if (opts.perMessageDeflate.serverNoContextTakeover === false) {
flags |= uws.SLIDING_DEFLATE_WINDOW;
}
}
const group = native.server.group.create(flags, opts.maxPayload);
let upgradeReq = null;
native.server.group.onConnection(group, (socket) => {
const spark = new this.Spark(
upgradeReq.headers, // HTTP request headers.
upgradeReq, // IP address location.
url.parse(upgradeReq.url).query, // Optional query string.
null, // We don't have an unique id.
upgradeReq, // Reference to the HTTP req.
socket // Reference to the socket.
);
let brokerConnection: TcpSocket = new TcpSocket(this.options.brokerPort, '127.0.0.1')
brokerConnection.on('error', (err: any) => logError('Worker' + ', PID ' + process.pid + '\n' + err.stack + '\n'))
brokerConnection.on('message', (msg: any) => msg === '#0' ? brokerConnection.send('#1') : this.socketServer.emitter.emit('#publish', JSON.parse(msg)))
brokerConnection.on('disconnect', () => logError('Something went wrong broker has been disconnected'))
this.socketServer.middleware = {}
this.socketServer.emitter = new EventEmitter()
this.socketServer.on = (event: string, fn: any) => this.socketServer.emitter.on(event, fn)
this.socketServer.publish = (channel: string, data: any) => {
brokerConnection.send(brokerMessage(channel, data))
this.socketServer.emitter.emit('#publish', { channel: channel, data: data })
}
this.httpServer = createServer().listen(this.options.port)
let uWS: any = new uws.Server({ server: this.httpServer })
uWS.on('connection', (socket: any) => this.socketServer.emitter.emit('connection', new Socket(socket, this)))
this.options.worker.call(this)
process.send(processMessages('ready', process.pid))
}
}
app.use('/api', jwt);
const port = 3000;
require('./routes')(app)
app.use((req, res, next)=>{
res.status(404).json({error: 'Route not found'});
})
const server = http.createServer(app)
const io = socket.listen(server);
io.engine.ws = new (require('uws').Server)({
noServer: true,
clientTracking: false,
perMessageDeflate: false
});
require('./_quarks/socket-io')(io)
server.listen(3000, () => console.log(`Init Server to port: ${port} `));