Skip to content

Instantly share code, notes, and snippets.

@suru-dissanaike
Created December 8, 2020 08:33
Show Gist options
  • Select an option

  • Save suru-dissanaike/8c5e5b54ad34deeb45ef300a54d7ea29 to your computer and use it in GitHub Desktop.

Select an option

Save suru-dissanaike/8c5e5b54ad34deeb45ef300a54d7ea29 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const WebSocket = require('ws');
const https = require('https');
const server = new https.createServer({
cert: fs.readFileSync('server.crt'),
key: fs.readFileSync('server.key')
});
const wss = new WebSocket.Server({server});
wss.on('connection', function connection(ws) {
ws
.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('hello from server!, the time is: ' + timestamp());
});
});
//Helper function to create a timestamp
function timestamp() {
return (new Date)
.toISOString()
.replace(/z|t/gi, ' ')
.trim()
};
//Start the server
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment