From 8497675a5272939724da0fb86670a2bbfee42af9 Mon Sep 17 00:00:00 2001 From: Muaz Khan Date: Mon, 22 Apr 2019 18:38:12 +0500 Subject: [PATCH] UPDATED: websocket-over-nodejs and socketio-over-nodejs --- socketio-over-nodejs/README.md | 145 +---------------------------- socketio-over-nodejs/package.json | 9 +- socketio-over-nodejs/server.js | 99 ++++++++++++++++++++ websocket-over-nodejs/README.md | 10 ++ websocket-over-nodejs/package.json | 4 +- websocket-over-nodejs/server.js | 5 +- 6 files changed, 120 insertions(+), 152 deletions(-) create mode 100644 socketio-over-nodejs/server.js diff --git a/socketio-over-nodejs/README.md b/socketio-over-nodejs/README.md index 6ca332b3..77e7677b 100644 --- a/socketio-over-nodejs/README.md +++ b/socketio-over-nodejs/README.md @@ -8,121 +8,14 @@ -= +#### `server.js` file -#### Install via `npm` +This is the file that I was running on `https://webrtcweb.com:9559/`. I'll recommend to modify and use this file. ``` npm install socketio-over-nodejs ``` -and run the `signaler.js` nodejs file: - -``` -// run simple HTTP server -node ./node_modules/socketio-over-nodejs/signaler.js - -// run HTTPs server -node ./node_modules/socketio-over-nodejs/signaler-ssl.js -``` - -Now you can test: `http://localhost:8888/` or `https://localhost:8888/` - -You can use ip-address `127.1.1` on Mac/Linux instead of `localhost`. - -= - -### Install on Linux/Ubuntu/CentOS/Debian/Mac etc. - -``` -# create a directory -mkdir socketio-over-nodejs - -# open directory -cd socketio-over-nodejs - -# get package -wget http://webrtcweb.com/socketio-over-nodejs.tar - -# extract package -tar -xf socketio-over-nodejs.tar - -# run simple node.js server -node signaler.js - -# run HTTPs server -node signaler-ssl.js -``` - -# Download ZIP on windows - -http://webrtcweb.com/websocket-over-nodejs.zip - -Now, you can open port `8888` on your ip address/domain; or otherwise on localhost: `http://localhost:8888/` - -= - -It is using port `8888`; you can edit this port using following commands: - -``` -vi signaler.js - -# now edit port 8888 -# and save changes & quit - -# press "insert" key; then press "Esc" key and the type -:wq -``` - -`:wq` command saves changes in the file and exits editing mode. If you don't want to save changes; simply type: - -``` -# if you don't want to save changes however want to exit editing mode -:q -``` - -Common Error: `Error: listen EADDRINUSE`. It means that same port is used by another application. You can close all existing processes running on same port: - -``` -// list all active processes running on same port -fuser -v 8888/tcp - -// kill all processes running on port "8888" -fuser -vk 8888/tcp - -// list again to verify closing ports -fuser -v 8888/tcp -``` - -You can delete "directory" and re-install: - -``` -rm -rf socketio-over-nodejs -mkdir socketio-over-nodejs - -# and following above steps to "wget" and "tar" then "node" to run! -``` - -= - -1. https://www.webrtc-experiment.com/docs/WebRTC-Signaling-Concepts.html -2. http://www.RTCMultiConnection.org/FAQ/ -3. http://www.RTCMultiConnection.org/docs/sessionid/ -4. http://www.RTCMultiConnection.org/docs/channel-id/ - -= - -Otherwise, follow these steps: - -1. Download and extract **ZIP file** of this repository then copy `folder-location` of the`signaler.js` file -2. Open **Node.js command prompt** window -3. Type command `cd folder-location` where `folder-location` can be `C:\socketio-over-nodejs` -4. Type `npm install express` or [download ZIP](http://code.snyco.net/node_modules/express.zip) -5. Type `npm install socket.io` or [download ZIP](http://code.snyco.net/node_modules/socket.io.zip) -6. Type `node signaler` to run the node.js server - -Then open `http://localhost:8888/`. - # How to use? In `ui.js` files you can find `openSocket` method; or in all libraries; you can find `openSignalingChannel` method. @@ -211,38 +104,6 @@ function testChannelPresence(channel) { testChannelPresence('default-channel'); ``` -# What is `cd folder-location`? - -Using this command; you can open project's directory (i.e. folder). - -# What is `node signaler`? - -This command runs node.js server via `signaler.js` file. That file handles socket.io relevant stuff. - -# Note - -Each experiment is using something like this: - -```javascript -var SIGNALING_SERVER = '/'; -``` - -This is the URL of your site. By default it will be equal to `http://localhost:8888/`. - -It is strongly recommended to use absolute URL including port number: - -```javascript -var SIGNALING_SERVER = 'http://domain.com:8888/'; -``` - -# Signaling Concepts - -Interested to understand WebRTC Signaling Concepts? Read [this document](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs/Signaling-Concepts.md). - -# SSL/TLS/HTTPS/Socket.io/Node.js ?? - -https://github.com/muaz-khan/WebRTC-Experiment/issues/62 - # License -[Socket.io over Node.js](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs) is released under [MIT licence](https://www.webrtc-experiment.com/licence/) . Copyright (c) [Muaz Khan](https://plus.google.com/+MuazKhan). +[Socket.io over Node.js](https://github.com/muaz-khan/WebRTC-Experiment/blob/master/socketio-over-nodejs) is released under [MIT licence](https://www.webrtc-experiment.com/licence/) . Copyright (c) [Muaz Khan](https://MuazKhan.com). diff --git a/socketio-over-nodejs/package.json b/socketio-over-nodejs/package.json index f861c1aa..707d7249 100644 --- a/socketio-over-nodejs/package.json +++ b/socketio-over-nodejs/package.json @@ -2,7 +2,7 @@ "name": "socketio-over-nodejs", "subdomain": "socketio-over-nodejs", "preferGlobal": true, - "version": "3.0.2", + "version": "3.0.3", "author": { "name": "Muaz Khan", "email": "muazkh@gmail.com" @@ -15,9 +15,9 @@ } ], "scripts": { - "start": "node signaler.js" + "start": "node server.js" }, - "main": "signaler.js", + "main": "server.js", "repository": { "type": "git", "url": "https://github.com/muaz-khan/WebRTC-Experiment.git" @@ -46,8 +46,7 @@ "node": ">=0.6" }, "dependencies": { - "socket.io": "0.9.x", - "node-static": "0.7.x" + "socket.io": "0.9.x" }, "bugs": { "url": "https://github.com/muaz-khan/WebRTC-Experiment/issues" diff --git a/socketio-over-nodejs/server.js b/socketio-over-nodejs/server.js new file mode 100644 index 00000000..cd9e1a3b --- /dev/null +++ b/socketio-over-nodejs/server.js @@ -0,0 +1,99 @@ +// https://www.webrtc-experiment.com/ + +var fs = require('fs'); + +// don't forget to use your own keys! +var options = { + // key: fs.readFileSync('fake-keys/privatekey.pem'), + // cert: fs.readFileSync('fake-keys/certificate.pem') + key: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/privkey.pem'), + cert: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/fullchain.pem') +}; + +// HTTPs server +var app = require('https').createServer(options, function(request, response) { + response.writeHead(200, { + 'Content-Type': 'text/html' + }); + var link = 'https://github.com/muaz-khan/WebRTC-Experiment/tree/master/socketio-over-nodejs'; + response.write('socketio-over-nodejs

socketio-over-nodejs

var socket = io.connect("https://webrtcweb.com:9559/");
'); + response.end(); +}); + + +// socket.io goes below + +var io = require('socket.io').listen(app, { + log: true, + origins: '*:*' +}); + +io.set('transports', [ + // 'websocket', + 'xhr-polling', + 'jsonp-polling' +]); + +var channels = {}; + +io.sockets.on('connection', function (socket) { + var initiatorChannel = ''; + if (!io.isConnected) { + io.isConnected = true; + } + + socket.on('new-channel', function (data) { + if (!channels[data.channel]) { + initiatorChannel = data.channel; + } + + channels[data.channel] = data.channel; + onNewNamespace(data.channel, data.sender); + }); + + socket.on('presence', function (channel) { + var isChannelPresent = !! channels[channel]; + socket.emit('presence', isChannelPresent); + }); + + socket.on('disconnect', function (channel) { + if (initiatorChannel) { + delete channels[initiatorChannel]; + } + }); +}); + +function onNewNamespace(channel, sender) { + io.of('/' + channel).on('connection', function (socket) { + var username; + if (io.isConnected) { + io.isConnected = false; + socket.emit('connect', true); + } + + socket.on('message', function (data) { + if (data.sender == sender) { + if(!username) username = data.data.sender; + + socket.broadcast.emit('message', data.data); + } + }); + + socket.on('disconnect', function() { + if(username) { + socket.broadcast.emit('user-left', username); + username = null; + } + }); + }); +} + +// run app + +app.listen(process.env.PORT || 9559); + +process.on('unhandledRejection', (reason, promise) => { + process.exit(1); +}); + +console.log('Please open SSL URL: https://localhost:'+(process.env.PORT || 9559)+'/'); diff --git a/websocket-over-nodejs/README.md b/websocket-over-nodejs/README.md index 26afda65..ed5dcba4 100644 --- a/websocket-over-nodejs/README.md +++ b/websocket-over-nodejs/README.md @@ -2,6 +2,16 @@ [![npm](https://img.shields.io/npm/v/websocket-over-nodejs.svg)](https://npmjs.org/package/websocket-over-nodejs) [![downloads](https://img.shields.io/npm/dm/websocket-over-nodejs.svg)](https://npmjs.org/package/websocket-over-nodejs) +#### `server.js` file + +This is the file that I was running on `wss://webrtcweb.com:9449/`. I'll recommend to modify and use this file. + +```sh +npm install ebsocket-over-nodejs +``` + +#### Explanation + This repository has following kinds of browser-based demos: 1. Text Chat using rooms diff --git a/websocket-over-nodejs/package.json b/websocket-over-nodejs/package.json index ea884634..c2475afc 100644 --- a/websocket-over-nodejs/package.json +++ b/websocket-over-nodejs/package.json @@ -1,7 +1,7 @@ { "name": "websocket-over-nodejs", "preferGlobal": true, - "version": "0.0.4", + "version": "0.0.6", "author": { "name": "Muaz Khan", "email": "muazkh@gmail.com" @@ -36,7 +36,7 @@ "bugs": { "url": "https://github.com/muaz-khan/WebRTC-Experiment/issues" }, - "homepage": "https://webrtcweb.com:9449/", + "homepage": "https://github.com/muaz-khan/WebRTC-Experiment/tree/master/websocket-over-nodejs", "_id": "websocket-over-nodejs@", "_from": "websocket-over-nodejs@" } diff --git a/websocket-over-nodejs/server.js b/websocket-over-nodejs/server.js index 85233a7c..ec795adb 100644 --- a/websocket-over-nodejs/server.js +++ b/websocket-over-nodejs/server.js @@ -15,9 +15,8 @@ var fs = require('fs'); var options = { // key: fs.readFileSync('fake-keys/privatekey.pem'), // cert: fs.readFileSync('fake-keys/certificate.pem') - key: fs.readFileSync('/home/ssl/webrtcweb.key'), - cert: fs.readFileSync('/home/ssl/server.csr'), - ca: fs.readFileSync('/home/ssl/inter.csr') + key: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/privkey.pem'), + cert: fs.readFileSync('/etc/letsencrypt/live/webrtcweb.com/fullchain.pem') }; // HTTPs server