Created
April 11, 2018 13:04
-
-
Save jbarros35/96a82451a46258b30a3135dbeeddbeef to your computer and use it in GitHub Desktop.
Open Websocket into Node JS server
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
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