braid/dist/server/server.js
2019-02-28 23:21:06 -05:00

41 lines
1.5 KiB
JavaScript
Executable File

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express");
const https = require("https");
const WebSocket = require("ws");
const fs = require("fs");
var privateKey = fs.readFileSync('certs/key.pem', 'utf8');
var certificate = fs.readFileSync('certs/cert.pem', 'utf8');
var credentials = { key: privateKey, cert: certificate };
const app = express();
const server = https.createServer(credentials, app);
const wss = new WebSocket.Server({ server, maxPayload: 250000 });
wss.on('connection', (ws) => {
console.log('client connected');
ws.on('message', (message) => {
console.log('received: %s', message);
const broadcastRegex = /^broadcast\:/;
if (broadcastRegex.test(message)) {
message = message.replace(broadcastRegex, '');
//send back the message to the other clients
wss.clients
.forEach(client => {
if (client != ws) {
client.send(`Hello, broadcast message -> ${message}`);
}
});
}
else {
ws.send(`Hello, you sent -> ${message}`);
}
});
ws.send('Hi there, welcome to braid, Measures Web Socket server.\nConnecting all our services!');
});
// start our server
server.listen(process.env.PORT || 8443, () => {
console.log(`Server started :)`);
});
app.get('/', (req, res) => {
res.send('Braid v0.1.2 is running!\n');
});
//# sourceMappingURL=server.js.map