Https server with websockets on node.js -
i'm trying create simple https server websockets support. had functional code https server , websockets, both standalone. don't know how combine it. prefer use free use libraries (mit,..)
i want able serve http request , websockets.
https example upgrade, don't know how handle upgraded websocket connection second example bellow this.
connection. process.env.node_tls_reject_unauthorized = "0"; var https = require('https'); var websocket = require('websocket'); var fs = require('fs'); var options = { key:fs.readfilesync('./cert/server.key'), cert:fs.readfilesync('./cert/server.crt') }; var server = https.createserver( options , function(req,res) { console.log('req'); res.writeheader(200, { 'content-type': 'text/plain' }); res.write('test'); res.end(); } ); server.on('upgrade', (req, socket, head) => { console.log('upgrade '); socket.write('http/1.1 101 web socket protocol handshake\r\n' + 'upgrade: websocket\r\n' + 'connection: upgrade\r\n' + '\r\n'); socket.pipe(socket); }); server.listen(8080, '127.0.0.1', () => { // make request const options = { port: 8080, hostname: '127.0.0.1', headers: { 'connection': 'upgrade', 'upgrade': 'websocket' } }; const req = https.request(options); req.end(); req.on('upgrade', (res, socket, upgradehead) => { console.log('got upgraded!'); *** here *** /* socket.end(); process.exit(0); */ }); });
this functional websockets without possibility handle simple http(s) requests.
var https = require('https'); var ws = require('websocket').server; var fs = require('fs'); var options = { key:fs.readfilesync('./cert/server.key'), cert:fs.readfilesync('./cert/server.crt') }; var server = https.createserver( options , function(req,res) { res.writeheader(200); res.end(); } ); server.listen(8080); var wss = new ws({httpserver:server}); var connectionnumber=0; console.log('start '); wss.on('request',function(req){ req.on('requestaccepted',function(conn){ conn.on('message',function(msg){ conn.send('test'); }); conn.on('close',function(msg){ }); }); req.accept(null,req.origin); });
i looking functional example of combined solution haven't luck.
you can have wss-connection trouble self-signed certificate (code 1006
). in case must add certificate root ca (firefox, chrome).
// app.js 'use strict' const https = require('https'); const fs = require('fs'); const ws = require('ws'); const options = { key: fs.readfilesync('key.pem'), cert: fs.readfilesync('cert.pem') }; const index = fs.readfilesync('./index.html'); let server = https.createserver(options, (req, res) => { res.writehead(200); res.end(index); }); server.addlistener('upgrade', (req, res, head) => console.log('upgrade:', req.url)); server.on('error', (err) => console.error(err)); server.listen(8000, () => console.log('https running on port 8000')); const wss = new ws.server({server, path: '/echo'}); wss.on('connection', function connection(ws) { ws.send('hello'); ws.on('message', (data) => ws.send('receive: ' + data)); });
// index.html <!doctype html> <html> <head> <meta charset="utf-8"/> </head> <body> <script> var socket = new websocket('wss://127.0.0.1:8000/echo'); socket.onopen = () => console.log('connected') || setinterval(() => socket.send(new date().tolocalestring()), 1000); socket.onclose = (event) => console.log((event.wasclean) ? 'disconnected' : 'connection break: ' + (event.reason || event.code)); socket.onmessage = (event) => console.log('data', event.data); socket.onerror = (err) => console.error(err.message); </script> press f12 open console... </body> </html>
Comments
Post a Comment