Created
December 8, 2020 08:33
-
-
Save suru-dissanaike/8c5e5b54ad34deeb45ef300a54d7ea29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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