Skip to content

Instantly share code, notes, and snippets.

@jbarros35
Created April 11, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarros35/96a82451a46258b30a3135dbeeddbeef to your computer and use it in GitHub Desktop.
Save jbarros35/96a82451a46258b30a3135dbeeddbeef to your computer and use it in GitHub Desktop.
Open Websocket into Node JS server
require('dotenv').config();
var http = require('http');
var https = require('https');
var fs = require('fs');
const WebSocket = require('ws');
Tail = require('tail').Tail;
var privateKey = fs.readFileSync('./key.pem', 'utf8');
var certificate = fs.readFileSync('./cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
var certpass = process.env.ENV_CERTPASS;
var options = {
key: privateKey,
cert: certificate,
passphrase: certpass
};
var server = https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
});
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws, req) {
const ip = req.connection.remoteAddress;
console.log('client ip'+ip);
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something your ip '+ip);
});
server.listen(443);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment